Recently I had what seems to be a common question for Django regarding model inheritance. I have a bunch of different models that I want to display either individually or as a group. Read as: query the database for everything in it, or just a certain category (model) of items.
Ultimately, I settled on multi-table-inheritance. My models look something like:
class Unit(models.Model):
etc, etc...
class Child1(Unit):
etc, etc...
class Child2(Unit):
etc, etc...
So now I can query units to get everything and individual children to get more specific. It works fine, but I just looked at the SQL generated for a list of all units...it gets pretty ugly!
SELECT "all the fields from all the tables (Unit and Children)"
FROM "Unit"
LEFT OUTER JOIN "Child1" ON ("whatever")
LEFT OUTER JOIN "Child2" ON ("whatever")
LEFT OUTER JOIN "Child3" ON ("whatever")
LEFT OUTER JOIN "Child4" ON ("whatever")
LEFT OUTER JOIN "Child5" ON ("whatever")
Basically, each child of Unit will require another left outer join when I get an index view of all items.
Assuming I have at most 5 children of Unit and that there will be at most 200 items in the DB, is this a deal-breaker? I can always just cache the index view, yes? Or am I missing another problem with MTI? The book "2 Scoops of Django" is absolutely against using multi-table-inheritance...to the point of saying "don't do it ever". But I feel like it solves my problem, is easily understood, and is almost a necessary evil given how Django handles relations.
Keep what I have or go back to the drawing board?