Is there a way to make only one query of a super class then get a list of all the respective sub classes in django? [duplicate]

StackOverflow https://stackoverflow.com//questions/9674046

Question

Possible Duplicate:
Django Model Inheritance query a central table

Following along the officail docs found here
https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance

I see that I can query Place which is a Super of Restaurant via

p = Place.objects.filter(name="Bob's Cafe")

So at that point is there a way to check and see if the objects returned are also of type sub class Restaurant and if so access their sub class fields?

At the moment all I can tell is that all the objects returned are of Place class and their sub class properties and methods are not available since the object is not being cast as its sub class.

If this methodology is not possible then what is the best practice went trying to abstract a super class and minimize the overhead of queries when trying to get a list containing all the subclass models? I understand in theory I could do do a query of each sub class then combined the list together but then I would have to do more if I wanted to order that list by say a date.

Was it helpful?

Solution

So at that point is there a way to check and see if the objects returned are also of type sub class Restaurant and if so access their sub class fields?

If you've queried the Place model, the objects returned are Place instances that have no knowledge of any MTI-associated subclasses. You need to provide a field on the superclass to link downward to the subclass. See Making a multi-table inheritance design generic in Django

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top