I was trying to refactor my models to reuse the code for defining and saving slugs like so:
class SlugMixin:
slug = models.SlugField(null=True, blank=True)
def save(self, *args, **kwargs):
# Take the __str__ output and slugify it
self.slug = slugify(self)
super().save(*args, **kwargs)
class Meta:
abstract = True
...
class SomeModel(SlugMixin, models.Model):
name = models.CharField(...
def __str__(self):
return self.name
...
And in my admin.py Iâm trying to:
class SomeModelAdmin:
prepopulated_fields = {"slug": ("name",)}
However, Iâm getting this error:
<class âsomeapp.admin.SomeModelAdminâ>: (admin.E027) The value of âprepopulated_fieldsâ refers to âslugâ, which is not a field of âsomeapp.SomeModelâ.
If I move the slug field definition into my model the code works as intended. I can still use the SlugMixin and its save method from the mixin, but defining the actual slug field definition right in the mixin makes the admin code reject it with the error. Am I doing something wrong or is this an actual bug?