#29985: Add ModelAdmin.list_prefetch_related -------------------------------------+------------------------------------- Reporter: Hidde Bultsma | Owner: Hidde | Bultsma Type: New feature | Status: closed Component: contrib.admin | Version: master Severity: Normal | Resolution: wontfix Keywords: admin ModelAdmin | Triage Stage: prefetch_related | Unreviewed Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Hidde Bultsma): Hi Carlton, Thanks for your response, I haven't thought about using an annotation for the item count. That's indeed much better than doing a prefetch. I can think of different cases where instead a prefetch is needed in the changelist, such as displaying a list of items: "foo, bar, and item3". The decision is understandable, the `ModelAdmin` API is already huge, and a `list_prefetch_related` option is maybe too specific. So, unless I'm missing something, to do anything querysetwise just for the changelist, one needs to check the `resolver_match` property of the request: {{{#!python def get_queryset(self, request): qs = super().get_queryset(request) if request.resolver_match.view_name.endswith('changelist'): return qs.prefetch_related('items') return qs }}} Maybe a `get_list_queryset` method could help with this, but that would also add to the `ModelAdmin` API. -- Ticket URL: <https://code.djangoproject.com/ticket/29985#comment:3> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.3e57d63e49e08004a38234d0782edd7a%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.