django_polymorphic icon indicating copy to clipboard operation
django_polymorphic copied to clipboard

A hint for the polymorphic queryset set to prefetch popular/expected derived classes

Open IlyaSemenov opened this issue 16 years ago • 1 comments

Consider the classes Car(PolymorphicModel), Bus(Car) and Truck(Car). Right now, when you do a polymorphic query against Car.objects, and when it finds some trucks, addition SQL query(ies) are made, like:

... WHERE app_truck.car_ptr_id IN (2,5,10)

Sometimes the class structure is not too large and that might be useful to be able to give a hint to the query to prefetch the data for given popular or expected classes with a LEFT JOIN. Ideally, that would integrate with select_related, for instance: Car.object.select_related('truck') # automatically join "trucks" table and get rid of an additional query or even Car.object.select_related('truck', 'bus__school') # join against Truck, Bus, and School (which is referenced from Bus with a ForeignKey)

IlyaSemenov avatar Feb 17 '10 05:02 IlyaSemenov

Ilya, I believe the topic you raised is of more general interest and best discussed on the google group. Would you like to re-post this issue on the django_polymorphic google newsgroup? This way it might make your idea/feature request more visible and better allow discussion about it.

I looked into possibilities along the lines of your proposal in the past but did not yet find straightforward ways to implement it. But perhaps someone else with a deeper understanding of the ORM internals has ideas towards an implementation.

bconstantin avatar Feb 22 '10 17:02 bconstantin