Tuesday, March 3, 2015

Re: Can the new `Prefetch` solve my problem?

On Tue, Mar 3, 2015 at 5:59 AM, Simon Charette <charette.s@gmail.com> wrote:
> Hi cool-RR,
>
> The following should do:
>
> filtered_chairs = Chair.objects.filter(some_other_lookup=whatever)
> desks = Desk.objects.prefetch_related(
> PrefetchRelated('chairs', filtered_chairs, to_attr='filered_chairs'),
> PrefetchRelated('nearby_chairs', filtered_chairs,
> to_attr='filtered_nearby_chairs'),
> )
>
> from itertools import chain
> for desk in desks:
> for chair in chain(desk.filtered_chairs, desk.filtered_nearby_chairs):
> # ....
>
> It will issue only three queries independently of the number of Chair or
> Desks you have.
>
> Simon

Does filtered_chairs get executed 'as-is', or is it then filtered by
the query to which it is attached? IE:

filtered_chairs = Chair.objects.filter(some_other_lookup=whatever)
desks = Desk.objects.filter(pk__range=(1,1000)).prefetch_related(
PrefetchRelated('chairs', filtered_chairs, to_attr='filered_chairs'),
PrefetchRelated('nearby_chairs', filtered_chairs,
to_attr='filtered_nearby_chairs'),
)

is filtered_chairs filtered by the 'desk__pk__range'?

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1JebBHf%3D2NyUTrMX6ad_DzavULpRvO2H991fgyL7mRfNA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment