如何使我的模型字段在Django中可选?

时间:2022-09-04 12:08:32

I'm trying to follow the full example at the bottom of

我试着按照下面的例子来做

https://docs.djangoproject.com/en/dev/topics/auth/customizing/

https://docs.djangoproject.com/en/dev/topics/auth/customizing/

In my model I have modified to the following

在我的模型中,我修改了以下内容

date_of_birth = models.DateField(null=True)

However when I try register a user I still get the following error message:

但是,当我尝试注册一个用户时,仍然会得到以下错误消息:

date_of_birth <ul class="errorlist"><li>This field is required.</li></ul>

In what other places do I need to make date_of_birth optional?

在其他地方,我需要使date_of_birth成为可选的吗?

1 个解决方案

#1


20  

You would have to add blank=True as well in field definition.

您必须在字段定义中添加blank=True。

date_of_birth = models.DateField(null=True, blank=True)

From modelform doc

从modelform医生

If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.

如果模型字段有blank=True,则required在表单字段上设置为False。否则,需要= True。

Don't forget to reset and sync DB again after changing this.

更改后不要忘记重新设置和同步DB。

#1


20  

You would have to add blank=True as well in field definition.

您必须在字段定义中添加blank=True。

date_of_birth = models.DateField(null=True, blank=True)

From modelform doc

从modelform医生

If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.

如果模型字段有blank=True,则required在表单字段上设置为False。否则,需要= True。

Don't forget to reset and sync DB again after changing this.

更改后不要忘记重新设置和同步DB。