在 Django 中,你可以使用 'DateTimeField' 字段的 'auto_now_add' 属性来设置默认值为当前时间。

在你的模型类中,定义一个 'DateTimeField' 字段,然后将 'auto_now_add' 属性设置为 'True',如下所示:

from django.db import models

class MyModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    # 其他字段...

这样,每次创建一个新的对象时,'created_at' 字段都会自动被设置为当前时间。

Django 数据库字段默认值设置:使用 auto_now_add 获取当前时间

原文地址: https://www.cveoy.top/t/topic/mLNp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录