关于外键:Django,当使用ForeignKey字段时,哪个更好是blank = True还是null = True?

Django, when using ForeignKey field, which is better blank=True or null=True?

来自:http://docs.djangoproject.com /恩/ 2 / / / / #域参考模型django.db.models.field.null

Avoid using null on string-based fields such as CharField and
TextField. If a string-based field has null=True, that means it has
two possible values for"no data": NULL, and the empty string. In most
cases, it’s redundant to have two possible values for"no data;" the
Django convention is to use the empty string, not NULL.

什么是更好的在场外键?外键是一个字符串为基础的场域,不是吗?

what is convention for ForeignKey in django.I Wonder

真的是更好的或空白的零= =真的是更好的?在性能上,或在任何方便。


这是不同的。见文件:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

所以对于foreignkey,您需要同时设置:null=True, blank=True


它们用于不同的目的:

  • 空=真:如果要使数据库中的列可以为空,则使用它。
  • 空白=真:在数据库列中不起作用。此列仍然不可为空(默认情况下为空),但在HTML表单中,输入字段可以保持为空。