I'd love to see this solved. I'm not sold on the API, but then again I've yet to see a single suggested API for this problem that I actually like.
I showed you this a little while ago, you had some reservations with my idea, but I figured I might as well add it to the conversation anyway: https://gist.github.com/AndrewIngram/5636041 Essentially, I'm wondering if extending the prefetch_related concept is the right approach, and whether it's instead worth exploring the more general idea of "attaching stuff to models at the ORM level". There're a lot of useful concepts from other ORMs related to attaching prebuilt data structures like arrays, dicts, calculated values, filtered annotations, etc to models. The problems all seem somewhat related, and I'm worried about the possibility of solving them all in completely different ways. Regards, Andy On 23 August 2013 16:03, Marc Tamlyn <[email protected]> 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) > > > Opinions are welcome! > > Marc > > -- > 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.
