I have a couple models using Multi-table Inheritance. like so.
class Group(models.Model):
title = models.CharField(unique=True,max_length=255)
class UserGroup(Group):
user = models.ForeignKey(User)
def save(self, *args, **kwargs):
self.title = self.user.username
return super(Group, self).save(*args, **kwargs)
UserGroups need to have unique titles and so do Groups. But It doesn’t matter if an UserGroup has the same title as a Group. Matter a fact this should happen in some cases. How can I do this?
Edit:
What if I used the save method to create an namespace.
class UserGroup(Group):
user = models.ForeignKey(User)
def save(self, *args, **kwargs):
self.title = "user:" + self.user.username
return super(UserGroup, self).save(*args, **kwargs)
class Group(models.Model):
title = models.CharField(unique=True,max_length=255)
def save(self, *args, **kwargs):
self.title = "group:" + self.title
return super(Group, self).save(*args, **kwargs)
Would this be a good solution?
What you need is for
Group.titleandUserGroup.titleto be in independent tables. The problem with your design is that in Django multi-table inheritance, child tables are never independent of their parent tables; you will need to make bothGroupandUserGroupinherit from another model, either concrete or abstract, and then have both children declare atitlefield.