开发者

Django: Can the value of ForeignKey be None?

I have a model called SimplePage in which I have this line:

category = models开发者_StackOverflow中文版.ForeignKey('Category', related_name='items',
                             blank=True, null=True)

I assumed this will allow me to have SimplePage instances that do not have a Category.

But for some reason, when I try to create a SimplePage in the Admin with no Category, I get:

IntegrityError at /admin/sitehelpers/simplepage/add/
sitehelpers_simplepage.category_id may not be NULL

What is this?


Could it possibly be that you added the null=True attribute after doing the syncdb for that model? Django won't change database tables, only create them. Check in your database if NULL is allowed for that column and change it manually.

Edit: starting with Django 1.7, this answer and the comments are not really valid anymore, since Django gained a fully featured migration framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜