在django中区分null = True,blank = True [英] differentiate null=True, blank=True in django
问题描述
当我们在django中添加数据库字段时,我们通常会写入 models.CharField(max_length = 100,null = True,blank = True)
。与 ForeignKey
, DecimalField
等相同。使用
-
null = True
只有 -
blank = True
仅 -
null = True
,blank = True $对于不同的(
CharField
,
$ c> ForeignKey , ManyToManyField
, DateTimeField
)字段。使用1/2/3的优点/缺点是什么?
null = True
在数据库列中设置
NULL
(与 NOT NULL
)。 Django字段类型的空值(例如 DateTimeField
或 ForeignKey
将被存储为 NULL
在数据库中。
blank = True
确定表单中是否需要该字段。这包括管理员和您自己的自定义表单。如果 blank = True
则不需要该字段,而如果 False
则该字段不能为空。 p>
两者的组合非常频繁,因为通常如果您要允许一个字段在表单中为空,那么您还需要您的数据库才能允许 NULL
该字段的值。例外情况是 CharField
s和 TextField
s,Django中永远不会保存为 NULL
。空值作为空字符串存储在数据库中(''
)。
几个示例: p>
models.DateTimeField(blank = True)#引发IntegrityError如果空白
models.DateTimeField(null = True )#NULL允许,但必须填写一个表单
显然这两个选项不会使用逻辑意义(尽管如此,如果您希望在表单中始终需要一个字段,但在交易时可选,则可能会为 null = True,blank = False
通过类似shell的对象)。
models.CharField(blank = True)#没问题,空白存储为''
models.CharField(null = True)#NULL允许,但永远不会设置为NULL
CHAR
和 TEXT
类型从不保存为 NULL
by Django,所以 null = True
是不必要的。但是,您可以手动将这些字段之一设置为无
以强制将其设置为 NULL
。如果您有必要的场景,您应该仍然包含 null = True
。
When we add a database field in django we generally write models.CharField(max_length=100, null=True, blank=True)
. The same is done with ForeignKey
, DecimalField
etc. What is the basic difference in having
null=True
onlyblank=True
onlynull=True
,blank=True
in respect to different (CharField
, ForeignKey
, ManyToManyField
, DateTimeField
) fields. What are the advantages/disadvantages of using 1/2/3?
null=True
sets NULL
(versus NOT NULL
) on the column in your DB. Blank values for Django field types such as DateTimeField
or ForeignKey
will be stored as NULL
in the DB.
blank=True
determines whether the field will be required in forms. This includes the admin and your own custom forms. If blank=True
then the field will not be required, whereas if it's False
the field cannot be blank.
The combo of the two is so frequent because typically if you're going to allow a field to be blank in your form, you're going to also need your database to allow NULL
values for that field. The exception is CharField
s and TextField
s, which in Django are never saved as NULL
. Blank values are stored in the DB as an empty string (''
).
A few examples:
models.DateTimeField(blank=True) # raises IntegrityError if blank
models.DateTimeField(null=True) # NULL allowed, but must be filled out in a form
Obviously those two options don't make logical sense to use (though, there might be a use case for null=True, blank=False
if you want a field to always be required in forms, but optional when dealing with an object through something like the shell.)
models.CharField(blank=True) # No problem, blank is stored as ''
models.CharField(null=True) # NULL allowed, but will never be set as NULL
CHAR
and TEXT
types are never saved as NULL
by Django, so null=True
is unnecessary. However, you can manually set one of these fields to None
to force set it as NULL
. If you have a scenario where that might be necessary, you should still include null=True
.
这篇关于在django中区分null = True,blank = True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!