在django中缺少所需的Charfield保存为空字符串,不会引发错误 [英] missing required Charfield in django is saved as empty string and do not raise an error
问题描述
如果我尝试在Django 1.10中保存不完整的模型实例,我会期望Django引发错误。这似乎不是这样的。
If I try to save incomplete model instance in Django 1.10, I would expect Django to raise an error. It does not seem to be the case.
models.py:
models.py:
from django.db import models
class Essai(models.Model):
ch1 = models.CharField(max_length=100, blank=False)
ch2 = models.CharField(max_length=100, blank=False)
所以我有两个字段不允许为空Django在MySQL表创建时应用默认行为 NOT NULL
限制。我希望Django在存储之前未设置其中一个字段时发生错误。
So I have two fields not allowed to be empty (default behavior, NOT NULL
restriction is applied by Django at MySQL table creation). I expect Django to rase an error if one of the fields is not set before storing.
但是,当我创建一个不完整的实例时,数据被存储很好: / p>
However, when I create an incomplete instance, the data is stored just fine:
>>> from test.models import Essai
>>> bouh = Essai()
>>> bouh.ch1 = "some content for ch1"
>>> bouh.save()
>>> bouh.id
9
>>> bouh.ch1
'some content for ch1'
>>> bouh.ch2
''
>>>
我会期望Django引发错误。如果我强制 ch2
到无
,则会引发错误:
I would have expected Django to raise an error. If I force ch2
to None
, however, it raises an error:
>>> bouh = Essai()
>>> bouh.ch1 = "some content for ch1"
>>> bouh.ch2 = None
>>> bouh.save()
Traceback (most recent call last):
(...)
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: test_essai.ch2
>>> bouh.id
>>> bouh.ch1
'some content for ch1'
>>> bouh.ch2
>>>
这是正常的行为吗?我错过了什么?为什么Django在这种简单的情况下不会将错误提升为默认行为?
Is this normal behavior? Did I miss something? Why is Django not raising an error as default behavior in this simple case?
感谢您的光!
编辑
我收到了一些回复,指出SQL空字符串
不等于NULL,如 Django中所述模型空白= False不起作用?
但是,如果我们看到ModelForm行为,django文档中似乎有一个不一致。
I got a few replies pointing out the fact that in SQL empty string ""
is not equivalent to NULL, as stated in Django model blank=False does not work?
However, if we look at ModelForm behavior, there seem to be a inconsistency in django doc.
见问题
跟进:在django中缺少所需的Charfield Modelform保存为空字符串,不会引发错误
推荐答案
看起来像你对于 [blank] [1]
代表
注意事项有轻微误解这不同于null。 null纯粹是
数据库相关,而空白与验证相关。如果一个字段有
blank = True,表单验证将允许输入一个空值。如果
字段为空= False,那么该字段将被要求。
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.
还有
避免在基于字符串的字段(如CharField和
TextField)上使用null,因为空字符串值将始终存储为空的
字符串,而不是空值。如果基于字符串的字段为null = True,那么
意味着它有两个可能的no data值:NULL和空的
字符串。在大多数情况下,对于
没有数据有两个可能的值是多余的;Django约定是使用空字符串,而不是NULL。
Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. 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.
所以在你的情况下,blank = false并不意味着django将拒绝为 ch1
或 ch2
。但是如果您使用ModelForm,它将拒绝让该字段留空。
Thus in your case, blank=false does not mean django will refuse to write an empty string for ch1
or ch2
. but it will refuse to let you leave that field blank if you use a ModelForm with it.
这篇关于在django中缺少所需的Charfield保存为空字符串,不会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!