Reputation: 6970
I have read few threads and know that for sure django can have multiple abstract classes. But pretty much all the samples I saw are...
class AbsOne(models.Model):
pass
class AbsTwo(models.Model):
pass
class AbsThree(AbsOne, AbsTwo):
pass
but what if I have something like...
class AbsOne(models.Model):
pass
class AbsTwo(AbsOne): // this actually inheritance AbsOne
pass
class AbsThree(AbsOne): // this inheritance AbsOne
pass
What if I need to inheritance both AbsTwo, AbsThree
but these two are also inheritance to the same parent.
class AbsFour(AbsTwo, AbsThree):
pass
Is this doable without any conflict or extra fields?
Thanks in advance.
Upvotes: 2
Views: 1122
Reputation: 985
Just as with Python’s subclassing, it’s possible for a Django model to inherit from multiple parent models. Keep in mind that normal Python name resolution rules apply. The first base class that a particular name (e.g. Meta) appears in will be the one that is used; for example, this means that if multiple parents contain a Meta class, only the first one is going to be used, and all others will be ignored.
https://docs.djangoproject.com/en/1.11/topics/db/models/#multiple-inheritance
It's possible but it has some restrictions like ( overriding fields in parent class and meta classes ) and in Django ORM or the model way of classes is a bit different in architecture than regular python inheritance, read carefully what the documentation mentions and try to keep it simple.
... The main use-case where this is useful is for “mix-in” classes: adding a particular extra field or method to every class that inherits the mix-in. Try to keep your inheritance hierarchies as simple and straightforward as possible so that you won’t have to struggle to work out where a particular piece of information is coming from
edited: added another quote :)
Upvotes: 3