17đ
Update: Since Django 1.4, this feature is built in: see prefetch_related.
First answer: donât waste time writing something like qbind until youâve already written a working application, profiled it, and demonstrated that N queries is actually a performance problem for your database and load scenarios.
But maybe youâve done that. So second answer: qbind() does what youâll need to do, but it would be more idiomatic if packaged in a custom QuerySet subclass, with an accompanying Manager subclass that returns instances of the custom QuerySet. Ideally you could even make them generic and reusable for any reverse relation. Then you could do something like:
Poll.objects.filter(category='foo').fetch_reverse_relations('choices_set')
For an example of the Manager/QuerySet technique, see this snippet, which solves a similar problem but for the case of Generic Foreign Keys, not reverse relations. It wouldnât be too hard to combine the guts of your qbind() function with the structure shown there to make a really nice solution to your problem.
23đ
Time has passed and this functionality is now available in Django 1.4 with the introduction of the prefetch_related() QuerySet function. This function effectively does what is performed by the suggested qbind
function. ie. Two queries are performed and the join occurs in Python land, but now this is handled by the ORM.
The original query request would now become:
polls = Poll.objects.filter(category = 'foo').prefetch_related('choice_set')
As is shown in the following code sample, the polls
QuerySet can be used to obtain all Choice
objects per Poll
without requiring any further database hits:
for poll in polls:
for choice in poll.choice_set:
print choice
- [Django]-Django Unique Together (with foreign keys)
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-How to add additional column to Django QuerySet
15đ
I think what youâre saying is, âI want all Choices for a set of Polls.â If so, try this:
polls = Poll.objects.filter(category='foo')
choices = Choice.objects.filter(poll__in=polls)
- [Django]-Django â Reverse for '' not found. '' is not a valid view function or pattern name
- [Django]-How to fix the django_sites table?
- [Django]-How to remove models from django?
1đ
I think what you are trying to do is the term âeager loadingâ of child data â meaning you are loading the child list (choice_set) for each Poll, but all in the first query to the DB, so that you donât have to make a bunch of queries later on.
If this is correct, then what you are looking for is âselect_relatedâ â see https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
I noticed you tried âselect_relatedâ but it didnât work. Can you try doing the âselect_relatedâ and then the filter. That might fix it.
UPDATE: This doesnât work, see comments below.
- [Django]-Generic many-to-many relationships
- [Django]-How can I have two foreign keys to the same model in Django?
- [Django]-Testing nginx without domain name