On 29/09/11 21:43, Alex Gaynor wrote: > When I did this externally a number of years ago, I basically subclassed > ManyToManyField, overrode a bunch of code (quite a bit of copy paste as > I recall), and it's related manager and made it return a custom > queryset, which used a cache off of the object, which was populated by > another custom queryset which overrode iterator() in order to do the > bulk loading.
I've had another go, and I think it should be a lot more convincing now. https://code.djangoproject.com/attachment/ticket/16937/prefetch_3.diff It supports arbitrary depth, so you can do things like: User.objects.prefetch_related('groups__permissions') and it will also traverse singly-related objects (foreign key and one-to-one) in the chain. The implementation is significantly nicer, as it splits up responsibilities between QuerySet and the related managers the way it ought to be. One piece of evidence that the separation is good is that I implemented support for GenericRelations as a final step, and did so without touching the core 'prefetch_related' code at all - I simply added some methods to the manager produced by GenericRelation. This also means that the implementation could provide support for things like GenericRelation but in 3rd party apps. The implementation can also cope with the presence of a prefetch_related() fields already on any prefetched QuerySets (which can happen if a default manager has used prefetch_related), and will simply fold in the work to optimize the queries. With these things in place, it was literally a couple of lines to take one of my admin pages from 176 queries to 8. I think the combination of features here makes it a *very* compelling alternative to doing it outside core. Have I convinced you Alex? :-) I would like at some point to tackle the ability to prefetch objects with custom filters, as discussed on the ticket, rather than just the 'all()' case. However, that can wait, and I'd like some people to have a bash on this and find the bugs. It would be really nice if this could get in before the 1.4 alpha, because it has turned out to be a pretty neat feature I think. Regards, Luke -- "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife." (Jane Austen) Luke Plant || http://lukeplant.me.uk/ -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
