I use a voting app (django-ratings if that makes any difference) which uses django’s GenericForeignKey, has a ForeignKey to User, and several other fields like date of latest change.
I’d like to get all the objects of one content type, that a single user voted for ordered by date of latest change. As far as I understand – all the info can be found in a single table (except the content_type which can be prefetched/cached). Unfortunately django still makes an extra query each time I request a content_object.
So the question is – how do I get all the votes on a given model, by a given user, with related objects and given ordering with minimum database hits?
Edit: Right now I’m using 2 queries – first selecting all the votes, getting all the objects I need, filtering by .filter(pk__in=obj_ids) and finally populating them to votes objects. But it seems that a reverse generic relation can help solve the problem
Well right now we’re using prefetch_related() from django 1.4 on a GenericRelation. It still uses 2 queries, but has a very intuitive interface.