没有空字符串的Django CharField [英] Django CharField without empty strings

查看:36
本文介绍了没有空字符串的Django CharField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以制作一个不接受空字符串的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆