[Answer]-Django AutoSlugField across inherited models

1๐Ÿ‘

โœ…

OK so after digging through the source code it turns out the docs have updated since I lasted looked.

So, if you add objects = models.Manager() to your BaseModel and pass that to the AutoSlugField. This will check the slug against BaseModel rather than the sub class.

class BaseModel(models.Model):
    objects = models.Manager()
    name = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='name', unique=True, db_index=True, managers=objects)
    # ... other fields
๐Ÿ‘คrockingskier

0๐Ÿ‘

In database you have only one slug field in BaseModel table, HamsterModel and ElderberryModel are connected to BaseModel using foreign key. The only way I see in this situation is remove unique=True and check uniqueness at model level in save method.

๐Ÿ‘คsneawo

Leave a comment