【发布时间】:2015-10-17 08:05:16
【问题描述】:
在我的一个 django 模型中,我有一个这样的字段:
modified = models.DateTimeField(auto_now=True)
我想,在为这个模型创建 ModelForm 时,我可以跳过这个字段,Django 会自动填充它。
我的模型表单:
class FooForm(forms.ModelForm):
class Meta:
model = Foo
fields = ['text', 'name', 'description'] # notice - modified field not included -
# should not be shown to the user
但是,即使它没有出现在表单中,但在提交时,在创建新对象时,我得到了一个异常:
IntegrityError at /url/ - null value in column "modified" violates not-null constraint
我怎样才能做到这一点?
【问题讨论】:
-
您是否在您的模型表单中排除了它?还是显示给用户
-
您需要提供更多详细信息 - 模型、视图、表单等。该字段中不应有空值,因此有些奇怪。
-
创建对象时会发生这种情况吗?然后你还必须将
auto_now_add设置为True。 -
@KlausD。这就是问题所在。如果您将其作为答案提交,我会接受。
-
您应该提供回答问题所需的相关代码。
标签: python django postgresql python-3.x django-1.8