3

I'm trying to extend my django abstract base model via inheritance, but django model's behavior that automatically sets abstract = True to abstract = False on any subclasses of abstract models is bothering me.

So the situation is

from django.db.models import Model
from django.db.models.base import ModelBase

Class TimeStampedModel(Model):
    created_time = DateTimeField()
    modified_time = DateTimeField()

    class Meta:
        abstract = True
        ordering = ('created_time',)
        get_latest_by = 'created_time'


class RecordModelMetaClass(ModelBase):
    # NOT IMPLEMENTED YET
    pass


class RecordModel(TimeStampedModel):
    __metaclass__ = RecordModelMetaClass

    recording_model = NotImplemented
    recording_fields = NotImplemented

Where the abstract TimeStampedModel is base model for abstract RecordModel.

The problem is that Django's metaclass ModelBase automatically converts RecordModel's abstract = True to abstract = False when RecordModel is defined in import time.

Is there any way to turn off this django's behavior?

2
  • what is ModelMase (to be corrected in ModelBase, BTW)? Commented May 12, 2015 at 8:12
  • 1
    @Pynchia It's a metaclass for django.db.models.Model Commented May 12, 2015 at 8:15

1 Answer 1

4

Yes, and this is documented:

If the child wants to extend the parent’s Meta class, it can subclass it.

In your case:

class RecordModel(TimeStampedModel):
    class Meta(TimestampedModel.Meta):
        abstract = True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.