python - Django - Multi-table inheritance - reverse relation to SubClass -
i appreciate following problem. lets use models django documentation illustrate situation.
models.py
from django.db import models class place(models.model): owner = djangomodels.foreignkey('owner', related_name='places') objects = passthroughmanager.for_queryset_class(placequeryset)() class restaurant(place): ... members ... objects = passthroughmanager.for_queryset_class(restaurantqueryset)() class pub(place): ... members ... objects = passthroughmanager.for_queryset_class(pubqueryset)() class owner(models.model): ... members ...
queryset.py
class placequeryset(djangomodels.query.queryset): .. common place filters ... class restaurantqueryset(placequeryset): .. restaurant specific filters ... class pubqueryset(placequeryset): .. pub specific filters ...
so can see above have 1 base model 'place' , 2 sub models 'restaurant' , 'pub'. base model 'place' has link 'owner' object. querysets (placequeryset, restaurantqueryset, pubqueryset) have same inheritance hierarchy models (place, restaurant, pub).
the challenge having somehow retrieve pubqueryset containing pub objects linked owner....
i know 1 way need add owned_by(owner) filter pubqueryset , call following:
pub.objects.owned_by(owner)
but has performance issues because '.objects' not use prefetched results , hits db.
my thoughts:
when call
# owner instance of owner class >>owner.places.all()
it returns
placequeryset[<place object ><place object ><place object >....]
but how
pubqueryset[<pub object ><pub object >]
???
i know select_subclasses() method django-model-utils(1) can downcast objects in queryset if this
# owner instance of owner class >>owner.places.select_subclasses('pub')
i placequeryset instances of pub class
placequeryset[<pub object ><pub object >]
but still receive placequeryset , not pubqueryset...
thank help
jano
Comments
Post a Comment