0

Which is better to use among all of the model inheritance and why? I do not have knowledge about it so I want to know which better, or more useful so that I will know which is I will use for that particular scenario.

  • abstract base classes
  • multi-table inheritance
  • proxy models
3
  • 1
    almost certainly depends on your usage. do you have some context? multi-table can really end up shooting you in the foot, so i'd make sure that's required before choosing that path. (inheritance doesn't really play well with databases) Commented Feb 9, 2013 at 9:59
  • I'm reading a book and they defined that 3 model inheritance. If I have so many fields that the same they use of course I will use model inheritance. But in my question is which better in your opnion? Commented Feb 9, 2013 at 10:02
  • It still depends on your usecase. To what extent are the inheriting models related, and to what extent would they be different? Do you want to be able to perform queries on the base model, or just on the inheriting models separately? Commented Feb 9, 2013 at 11:00

1 Answer 1

1

Each of these techniques provide different benefits. It really depends on what you need to do.

The best place to start is reading the model inheritance docs.

Abstract base classes

Use these if you're just trying to reduce the amount of code you write. If you know that several fields appear in numerous models then write a base class and inherit it.

Multi-table inheritance

This is useful if you want a concrete base class that can be queried and operated on - for example Base.objects.all() is a bit like seeing Child1.objects.all() and Child2.objects.all() in the same queryset.

Proxy Models

Use proxy models if all the fields are the same across each model. You get one db tuple for each object, but that db tuple can be used to represent either the parent or the proxy. This is quite powerful, and almost certainly the way to go if your fields are the same.

Sign up to request clarification or add additional context in comments.

3 Comments

But most developers said using multi-table inheritance is a bad thing. But why they add this as model inheritance, if this is bad?
You really need a use case before you can say whether its a good or bad solution. As you rightly point out, the django developers wouldn't have taken the time to implement it if it wasn't ever useful.
I would say it can be bad. Don't use multi-table inheritance unless you have a specific reason to do so. Don't think of them as "parent classes" and use them as freely as you would normal object oriented inheritance. One reason is that queries will have extra joins and be slower. Another reason is that it increases the "magic factor", and your models won't actually be stored in a single table like one might expect so database-level operations can become more challenging, and third party tools like south can get confused. And never go more than 1x deep if you do use them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.