Hi James,

I've read the docs but I still couldn't figure it out. My queryset works
great in production, I'm trying to optimize it because our pageloads are
too slow. I know how to use querysets in Django pretty well, I just don't
know how to use `Prefetch`.

Can you give me the solution for the simplified example I gave? This might
help me figure out what I'm not understanding. One thing that might be
unclear with the example I gave, is that I meant I want to get a queryset
for `Desk` where every desk has an attribute names `favorite_or_nearby_chairs`
which contains the queryset of chairs that I desrcibed, prefetched.


Thanks,
Ram.

On Wed, Feb 25, 2015 at 11:18 PM, James Schneider <[email protected]>
wrote:

> I assume that you are talking about the select_related() and
> prefetch_related() queryset methods?
>
> https://docs.djangoproject.com/en/1.7/ref/models/querysets/#select-related
>
> https://docs.djangoproject.com/en/1.7/ref/models/querysets/#prefetch-related
>
> Both of those sections have excellent examples, and detail what the
> differences are (primarily joins vs. separate queries, respectively).
>
> For better help, you'll need to go into more detail about the queries you
> are trying to make, what you've tried (with code examples if possible), and
> the results/errors you are seeing.
>
> In general, I would try to get an initial queryset working and gathering
> the correct results first before looking at optimizations such as
> select_related(). Any sort of pre-fetching will only confuse the situation
> if the base queryset is incorrect.
>
> -James
>
> On Wed, Feb 25, 2015 at 12:05 PM, cool-RR <[email protected]> wrote:
>
>> Hi guys,
>>
>> I'm trying to solve a problem using the new `Prefetch` but I can't figure
>> out how to use it.
>>
>> I have these models:
>>
>>     class Desk(django.db.models.Model):
>>         pass
>>
>>     class Chair(django.db.models.Model):
>>         desk = django.db.models.Foreignkey('Desk', related_name='chair',)
>>         nearby_desks = django.db.models.ManyToManyField(
>>             'Desk',
>>             blank=True,
>>         )
>>
>> I want to get a queryset for `Desk`, but it should also include a
>> prefetched attribute `favorite_or_nearby_chairs`, whose value should be
>> equal to:
>>
>>     Chair.objects.filter(
>>         (django.db.models.Q(nearby_desks=desk) |
>> django.db.models.Q(desk=desk)),
>>         some_other_lookup=whatever,
>>     )
>>
>> Is this possible with `Prefetch`? I couldn't figure out how to use the
>> arguments.
>>
>>
>> Thanks,
>> Ram.
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> 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/46d9fdb7-c008-4496-acda-ac7cb30b4a89%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/46d9fdb7-c008-4496-acda-ac7cb30b4a89%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/EuPduHjSNos/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> 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/CA%2Be%2BciVk7_6VBDoBE-qjLBwrBxiNeVdP6-fwwnOXV%3DvSA3HnCw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVk7_6VBDoBE-qjLBwrBxiNeVdP6-fwwnOXV%3DvSA3HnCw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CANXboVY7ffbsLKi5hCaNkGUNz%2B_A2vO-PueQfCGhB%3DJoKOERnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to