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?