在Django中区分null=True,space=True在Django中添加数据库字段时,通常会编写models.CharField(max_length=100, null=True, blank=True)..同样的事情也发生在ForeignKey, DecimalField有什么基本的区别null=True只blank=True只null=True, blank=True就不同的(CharField, ForeignKey, ManyToManyField, DateTimeField)田野。使用1/2/3的优点/缺点是什么?
3 回答
偶然的你
TA贡献1841条经验 获得超3个赞
null=True
NULL
NOT NULL
DateTimeField
ForeignKey
NULL
blank=True
blank=True
False
NULL
CharField
TextField
NULL
''
).
models.DateTimeField(blank=True) # raises IntegrityError if blankmodels.DateTimeField(null=True) # NULL allowed, but must be filled out in a form
null=True, blank=False
models.CharField(blank=True) # No problem, blank is stored as ''models.CharField(null=True) # NULL allowed, but will never be set as NULL
CHAR
TEXT
NULL
null=True
None
NULL
null=True
.
慕田峪9158850
TA贡献1794条经验 获得超7个赞
blank
& null
class Test(models.Model): charNull = models.CharField(max_length=10, null=True) charBlank = models.CharField(max_length=10, blank=True) charNullBlank = models.CharField(max_length=10, null=True, blank=True) intNull = models.IntegerField(null=True) intBlank = models.IntegerField(blank=True) intNullBlank = models.IntegerField(null=True, blank=True) dateNull = models.DateTimeField(null=True) dateBlank = models.DateTimeField(blank=True) dateNullBlank = models.DateTimeField(null=True, blank=True)
CREATE TABLE Test ( id serial NOT NULL, "charNull" character varying(10), "charBlank" character varying(10) NOT NULL, "charNullBlank" character varying(10), "intNull" integer, "intBlank" integer NOT NULL, "intNullBlank" integer, "dateNull" timestamp with time zone, "dateBlank" timestamp with time zone NOT NULL, "dateNullBlank" timestamp with time zone, CONSTRAINT Test_pkey PRIMARY KEY (id))
CREATE TABLE Test ( `id` INT(11) NOT NULL AUTO_INCREMENT, `charNull` VARCHAR(10) NULL DEFAULT NULL, `charBlank` VARCHAR(10) NOT NULL, `charNullBlank` VARCHAR(10) NULL DEFAULT NULL, `intNull` INT(11) NULL DEFAULT NULL, `intBlank` INT(11) NOT NULL, `intNullBlank` INT(11) NULL DEFAULT NULL, `dateNull` DATETIME NULL DEFAULT NULL, `dateBlank` DATETIME NOT NULL, `dateNullBlank` DATETIME NULL DEFAULT NULL)
添加回答
举报
0/150
提交
取消