samedi 29 novembre 2014

python - Django - multi-table héritage - inverse la relation à la sous-classe - Stack Overflow


I would appreciate your help with following problem. Lets use models from Django documentation to illustrate my 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):
... some members ...
objects = PassThroughManager.for_queryset_class(RestaurantQuerySet)()

class Pub(Place):
... some members ...
objects = PassThroughManager.for_queryset_class(PubQuerySet)()

class Owner(models.Model):
... some members ...

queryset.py


class PlaceQuerySet(DjangoModels.query.QuerySet):
.. common place filters ...

class RestaurantQuerySet(PlaceQuerySet):
.. restaurant specific filters ...

class PubQuerySet(PlaceQuerySet):
.. pub specific filters ...

So as you can see above I have one base model 'Place' and two sub models 'Restaurant' and 'Pub'. Base model 'Place' has a link to 'Owner' object. Querysets (PlaceQuerySet, RestaurantQuerySet, PubQuerySet) have the same inheritance hierarchy like models (Place, Restaurant, Pub).


The challenge I am having is to somehow retrieve PubQuerySet containing only Pub objects linked to certain owner....




I know that one way to do what I need is to add owned_by(owner) filter into PubQuerySet and call following:


Pub.objects.owned_by(owner)

but it has performance issues because '.objects' does not use prefetched results and it always hits DB.




My thoughts:


When I call


# owner is instance of Owner class
>>owner.places.all()

it returns


PlaceQuerySet[<Place object ><Place object ><Place object >....]

But how do I get


PubQuerySet[<Pub object ><Pub object >]

???


I know that with select_subclasses() method from django-model-utils(1) I can downcast objects in Queryset so if I do something like this


# owner is instance of Owner class
>>owner.places.select_subclasses('pub')

I would get PlaceQuerySet with instances of Pub class


PlaceQuerySet[<Pub object ><Pub object >]

but still I will receive PlaceQuerySet and not PubQuerySet...


Thank you very much for your help


Jano



I would appreciate your help with following problem. Lets use models from Django documentation to illustrate my 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):
... some members ...
objects = PassThroughManager.for_queryset_class(RestaurantQuerySet)()

class Pub(Place):
... some members ...
objects = PassThroughManager.for_queryset_class(PubQuerySet)()

class Owner(models.Model):
... some members ...

queryset.py


class PlaceQuerySet(DjangoModels.query.QuerySet):
.. common place filters ...

class RestaurantQuerySet(PlaceQuerySet):
.. restaurant specific filters ...

class PubQuerySet(PlaceQuerySet):
.. pub specific filters ...

So as you can see above I have one base model 'Place' and two sub models 'Restaurant' and 'Pub'. Base model 'Place' has a link to 'Owner' object. Querysets (PlaceQuerySet, RestaurantQuerySet, PubQuerySet) have the same inheritance hierarchy like models (Place, Restaurant, Pub).


The challenge I am having is to somehow retrieve PubQuerySet containing only Pub objects linked to certain owner....




I know that one way to do what I need is to add owned_by(owner) filter into PubQuerySet and call following:


Pub.objects.owned_by(owner)

but it has performance issues because '.objects' does not use prefetched results and it always hits DB.




My thoughts:


When I call


# owner is instance of Owner class
>>owner.places.all()

it returns


PlaceQuerySet[<Place object ><Place object ><Place object >....]

But how do I get


PubQuerySet[<Pub object ><Pub object >]

???


I know that with select_subclasses() method from django-model-utils(1) I can downcast objects in Queryset so if I do something like this


# owner is instance of Owner class
>>owner.places.select_subclasses('pub')

I would get PlaceQuerySet with instances of Pub class


PlaceQuerySet[<Pub object ><Pub object >]

but still I will receive PlaceQuerySet and not PubQuerySet...


Thank you very much for your help


Jano


0 commentaires:

Enregistrer un commentaire