[英]Django CharField without empty strings
Is there any way to make a CharField (or TextField) that doesn't accept empty strings?有没有办法制作不接受空字符串的 CharField(或 TextField)? I'm trying to use blank=False
but it's not working...我正在尝试使用blank=False
但它不起作用...
class Foo(models.Model):
title = models.CharField(max_length=124, blank=False)
Then after syncdb
I run python manage.py shell
, and I can type:然后在syncdb
之后我运行python manage.py shell
,我可以输入:
f = Foo()
f.save()
and it doesn't complain.它不会抱怨。 The object has a title with a value ''
.该对象有一个值为''
的标题。 I don't want this to happen, I want it to complain if Foo()
isn't explicitly given a non-empty string.我不希望这种情况发生,如果Foo()
没有明确给出非空字符串,我希望它抱怨。 Any way to do this?有没有办法做到这一点?
I'm using Django 1.6我正在使用 Django 1.6
Use a MinLengthValidator
:使用MinLengthValidator
:
from django.core.validators import MinLengthValidator
...
title = models.CharField(max_length=10, validators=[MinLengthValidator(1)])
According to the documentation , blank=False
is purely validation-related that works only on a form level.根据文档, blank=False
纯粹与验证相关,仅适用于表单级别。
See related threads:请参阅相关主题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.