[Answer]-Django abstract model field name choice

1๐Ÿ‘

โœ…

A good approach would be using a super class for the common fields, and a subclass for name and title.

class Slug(models.Model)
    slug = models.SlugField(db_index=True, unique=True)

    class Meta:
        abstract = True

class NamedSlug(Slug)
    name = models.CharField(max_length=30)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(NamedSlug, self).save(*args, **kwargs)

class TitledSlug(Slug)
    title = models.CharField(max_length=30)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(TitledSlug, self).save(*args, **kwargs)
๐Ÿ‘คJey

Leave a comment