没有空字符串的Django CharField [英] Django CharField without empty strings
问题描述
有什么方法可以制作一个不接受空字符串的CharField(或TextField)?我正在尝试使用 blank = False
,但是它不起作用...
Is there any way to make a CharField (or TextField) that doesn't accept empty strings? I'm trying to use blank=False
but it's not working...
class Foo(models.Model):
title = models.CharField(max_length=124, blank=False)
然后在 syncdb
之后运行 python manage.py shell
,然后输入:
Then after syncdb
I run python manage.py shell
, and I can type:
f = Foo()
f.save()
,它没有抱怨.该对象的标题值为''
.我不希望发生这种情况,如果没有明确给 Foo()
一个非空字符串,我希望它抱怨.有什么办法吗?
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. Any way to do this?
我正在使用Django 1.6
I'm using Django 1.6
推荐答案
According to the documentation, blank=False
is purely validation-related that works only on a form level.
查看相关主题:
这篇关于没有空字符串的Django CharField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!