在Django中如何使我的模型字段可以选择? [英] How can I make my model fields optional in Django?
本文介绍了在Django中如何使我的模型字段可以选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试按照
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>
在其他哪些地方我需要使date_of_birth可选?
In what other places do I need to make date_of_birth optional?
推荐答案
您必须在字段定义中添加 blank = True
。
You would have to add blank=True
as well in field definition.
date_of_birth = models.DateField(null=True, blank=True)
从 modelform doc
如果模型字段为空白= True,则表单字段上的必填项设置为False。否则,required = True。
If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.
不要忘记在更改此项后重新设置并同步DB。
Don't forget to reset and sync DB again after changing this.
这篇关于在Django中如何使我的模型字段可以选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文