Saturday, August 27, 2011

Combining queries? A "join" in Python?

I have a some models related link this:

A has a foreign key on B which has a foreign key on C

I also have D with a foreign key on B, and E with a foreign key of C

If I do A.filter(**args).select_related() I will get all the As, Bs,
and Cs I want.

How do I get the Ds with a foreign key on a B in my queryset
(preferably filtered) and all Es with a foreign key on C (also
preferably filtered)

The best I have been able to come up with is to query for all the the
Ds and Es I want, and combine them with the Bs and Cs in the view.

I have a similar problem with another site, except that there not
every B I want has an A with a foreignkey pointing to it, so I cannot
just do select_related on A.

I am not worried about doing an extra query or two per page. What I
want to avoid is doing an extra query for each object in a lengthy
list.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment