[Solved]-Django – Can I alter construction of field defined in abstract base model for specific child model?

13👍

One solution would be to dynamically construct your abstract base class. For example:

def get_slugger_model(**slug_kwargs):
    defaults = {
        'unique': True,
        'db_index': False
    }
    defaults.update(slug_kwargs)

    class MySluggerModel(models.Model):
        slug = AutoSlugField(**defaults)

        class Meta:
            abstract = True

    return MySluggerModel


class MyModel(get_slugger_model()):
    pass


class MyModel2(get_slugger_model(populate_from='name')):
    name = models.CharField(max_length=20)

0👍

Update: I started out with the following solution, which was ugly, and switched to Daniel’s solution, which is not. I’m leaving mine here for reference.

Here’s my Metaclass rat trap that seems to be working (without extensive testing yet).

class SluggerMetaclass(ModelBase):
    """
    Metaclass hack that provides for being able to define 'slug_from' and 
    'slug_db_index' in the Meta inner class of children of SluggerModel in order to set 
    those properties on the AutoSlugField
    """
    def __new__(cls, name, bases, attrs):
        # We don't want to add this to the SluggerModel class itself, only its children
        if name != 'SluggerModel' and SluggerModel in bases:
            _Meta = attrs.get('Meta', None)
            if _Meta and hasattr(_Meta, 'slug_from') or hasattr(_Meta, 'slug_db_index'):
                attrs['slug'] = AutoSlugField(
                    populate_from=getattr(_Meta, 'slug_from', None),
                    db_index=getattr(_Meta, 'slug_db_index', False),
                    unique=True
                )
                try:
                    # ModelBase will reject unknown stuff in Meta, so clear it out before calling super
                    delattr(_Meta, 'slug_from')
                except AttributeError: 
                    pass
                try:
                    delattr(_Meta, 'slug_db_index')
                except AttributeError: 
                    pass
            else:
                attrs['slug'] = AutoSlugField(unique=True, db_index = False)  # default

        return super(SlugSerializableMetaclass, cls).__new__(cls, name, bases, attrs)

The SlugModel looks basically like this now:

class SluggerModel(models.Model):
    __metaclass__ = SluggerMetaclass
    objects = SluggerManager()
    # I don't define the AutoSlugField here because the metaclass will add it to the child class.
    class Meta:
        abstract = True

And I can acheive the desired effect with:

class SomeModel(SluggerModel, BaseModel):
    name = CharField(...)
    class Meta:
        slug_from = 'name'
        slug_db_index = True

I have to put SluggerModel first in the inheritance list for models having more than one abstract parent model, or else the fields aren’t picked up by the other parent models and validation fails; however, I couldn’t decipher why.

I guess I could put this an answer to my own question, since it works, but I’m hoping for a better way since its a bit on the ugly side. Then again, hax is hax so what can you do, so maybe this is the answer.

Leave a comment