I think that restriction is reasonable.

So this wouldn't work:
Publisher.objects.prefetch_related('authors', to_attr='authors_by_age',
queryset=Author.objects.order_by('age')).prefetch_related('
authors_by_age__books')
But this would:
Publisher.objects.prefetch_related('authors').prefetch_related('authors__books',
to_attr='unpublished_books', queryset=Book.objects.filter(published=False))


On 26 August 2013 13:15, Anssi Kääriäinen <[email protected]> wrote:

> On Friday, August 23, 2013 6:03:13 PM UTC+3, Marc Tamlyn wrote:
>>
>> Ticket #17001 concerns the ability to customise the querysets used when
>> using `prefetch_related()`. This has a working implementation with a less
>> than ideal API - see the patches on the ticket.
>>
>> As the API design seems to be the main issue here, Anssi and I bounced
>> some ideas around on IRC, and I thought it best to propose our best
>> approach here to gather opinions on whether the API is palatable.
>>
>> Firstly note that the existing API would continue to work as it does now:
>> Publisher.objects.prefetch_**related('authors', 'authors__books')
>> You can then access publisher.authors.all() as before, each of which
>> will allow you to access author.books.all()
>>
>> If you wanted to prefetch the authors ordered by their age rather than
>> whatever the default is, you can do:
>> Publisher.objects.prefetch_**related('authors',
>> to_attr='authors_by_age', queryset=Author.objects.order_**by('age'))
>> The authors are now available as publisher.authors_by_age - this is a
>> list (not a qs) of the relevant Author objects.
>>
>> Only one custom field can be prefetched at once in this way. So if you
>> wanted to get the books prefetched as well, you would need to do:
>> Publisher.objects.prefetch_**related('authors',
>> to_attr='authors_by_age', queryset=Author.objects.order_**
>> by('age').prefetch_related('**books'))
>>
>> An alternative API that may work for this case is:
>> Publisher.objects.prefetch_**related('authors',
>> to_attr='authors_by_age', queryset=Author.objects.order_**
>> by('age')).prefetch_related('**authors_by_age__books')
>> i.e. the custom prefetch can be referenced in future chained prefetch
>> calls.
>>
>> This API can be used to customise the second level only:
>> Publisher.objects.prefetch_**related('authors').prefetch_**related('authors__books',
>> to_attr='unpublished_books', queryset=Book.objects.filter(**
>> published=False))
>>
>> If you want to customise both levels though, you must use the first
>> approach:
>> author_qs = Author.objects.order_by('age')**.prefetch_related('books',
>> to_attr='unpublished_books', queryset=Book.objects.filter(**
>> published=False))
>> Publisher.objects.prefetch_**related('authors',
>> to_attr='authors_by_age', queryset=author_qs)
>>
>
> My opinion: lets go forward with this API. Alternate is to add a dedicated
> method for custom queryset prefetches, so that prefetch_related() isn't
> overloaded. I am not sure if that is needed, so lets pass...
>
> I'd say that prefetching from outer query to custom inner query should
> never be allowed. This solves neatly issues like trying to prefetch into
> .values() query.
>
> Other opinions?
>
>  - Anssi
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" 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-developers.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to