Django: Could the value of ForeignKey be None?

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

 category = models.ForeignKey('Category', related_name='items', blank=True, null=True) 

I assumed that this would allow me to have SimplePage instances that have no category.

But for some reason, when I try to create SimplePage in Admin without a category, I get:

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

What is it?

+4
source share
1 answer

Maybe you added the null=True attribute after running syncdb for this model? Django will not modify database tables, just create them. Check your database if NULL allowed for this column and change it manually.

Edit : since Django 1.7, this answer and comments are no longer valid, since Django received a full-featured migration .

+9
source

Source: https://habr.com/ru/post/1300524/


All Articles