I have three different abstract model base classes . . . I'd like to use them in multiple inheritance, sort of like Mixins. Any problems with this?
E.g.,
class TaggableBase(models.Model): . . . class Meta: abstract = True class TimeStampedBase(models.Model): . . . class Meta: abstract = True class OrganizationalBase(models.Model): . . . class Meta: abstract = True class MyTimeStampedTaggableOrganizationalModel(OrganizationalBase, TimeStampedBase, TaggableBase): . . .
Django models support multiple inheritance.
An abstract model is a base class in which you define fields you want to include in all child models. Django doesn't create any database table for abstract models. A database table is created for each child model, including the fields inherited from the abstract class and the ones defined in the child model.
You can only inherit the implementation of one class by directly deriving from it. You can implement multiple interfaces, but you can't inherit the implementation of multiple classes.
“Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table.
If you use any fields at all in your class, inherit from models.Model
.
Otherwise Django will ignore those fields (the attributes will still be there in Python, but no fields will be created in the DB). Set abstract = True
to get "mixin" like behavior (i.e. no DB tables will be created for the mixins but for the models using those mixins).
If you don't use any fields, you can just inherit from object
, to keep things plain and simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With