I get "parent_id cannot be NULL" when creating my Django model

I create my own model Group ; I do not mean the built-in Group model. I want each group to be a member of another group (it’s the parent), but there is one β€œtop” group that does not have a parent group.

The admin interface will not allow me to create a group without entering a parent. I get the error personnel_group.parent_id may not be NULL . My Group model is as follows:

 class Group(models.Model): name = models.CharField(max_length=50) parent = models.ForeignKey('self', blank=True, null=True) order = models.IntegerField() icon = models.ImageField(upload_to='groups', blank=True, null=True) description = models.TextField(blank=True, null=True) 

How can i do this?

Thanks.

+4
source share
2 answers

I created the database before adding blank=True, null=True to the definition of the parent field. syncdb cannot handle this type of change, so Django did not collect my changes.

I deleted my database and let syncdb create another one and it worked perfectly.

+6
source

Django evolution will save you from this problem without dropping the complete database

0
source

All Articles