I practice django-mptt and face problem
Here is my models.py
class Genre(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
I create some data :
And now,I need to create 4F
under BuildingB
floor3 = Genre.objects.create(name="3F", parent= buildingB)
but face problem :
IntegrityError: duplicate key value violates unique constraint "myapp_genre_name_key"
DETAIL: Key (name)=(3F) already exists.
How can I fix it??
In my understanding, you want your names to be unique across the building.
To achieve that, remove the unique=True
from the name
field declaration and add a Meta option:
class Genre(MPTTModel):
name = models.CharField(max_length=50)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
class Meta:
unique_together = (('name', 'parent', ), )