[Django]-Django ForeignKey in abstract model class and multiple drived classes causes name clash

5๐Ÿ‘

โœ…

I simplified my example so much that I actually left out the cause of the error. I had actually two Foreign keys, and 8 error messages total. The ForeignKeys referred to the same entity, UserProfile: created_by, modified_by. The problem was that I used simply

related_name="%(class)s_set"

while I needed to distinguish these two

related_name="%(class)s_something_unique_set"

Like so:

class BlogEntryBase(models.Model):
    author = models.CharField(null=True, blank=True, max_length=100)
    title = models.CharField(null=True, blank=True, max_length=255)
    created_by = models.ForeignKey("main.UserProfile", verbose_name="Created By", related_name="%(class)s_created_by_set", blank=False, null=False)
    modified_by = models.ForeignKey("main.UserProfile", verbose_name="Modified By", related_name="%(class)s_modified_by_set", blank=False, null=False)

class CatBlogEntry(BlogEntryBase):
    pass

class DogBlogEntry(BlogEntryBase):
    pass
๐Ÿ‘คCsaba Toth

Leave a comment