Django + (django-model-utils): Combining two models / inheriting from two models

552 Views Asked by At

I recently learned about model inheritance in Django. I used it to great succes using the awesome package django-model-utils. I inherited from the TimeStampedModel and from the SoftDeletableModel. My problem is that I only managed to do the inheritance while inheriting from one model.

I now would like to inherit from both models at the same time. Is there a way to inherit from two models or to combine them? How would I best go about doing this?

PS: I've tried to combine them myself by putting the SoftDeletableQuerySetMixin in front of the TimeStampedModel in my model that inherits, but it broke things. Also I saw that django-model-utils comes with great tests out of the box and when I would succeed at combining them manually, I think I would have to write new tests for the combined model, wouldn't I? Is there a smart way to combine these models?

1

There are 1 best solutions below

0
On BEST ANSWER

You don't have to do anything special as you can extend from both models, such as:

class MyModel(TimeStampedModel, SoftDeletableModel):
    # You will automatically get created, modified, and is_removed
    ...