Django admin update get_queryset only for "change" view
In my django project admin I have an Order model. I display a bunch of information in the change page with the help of custom inlines. As I use a lot of related models, I want to prefetch/select the related objects but only for the change page (not the list page).
I need to update the get_queryset method with a conditional branch "if this is the change view" but I can't find a proper way to do that. Any ideas?
The easiest way is probably to check the request path. The path will end with "change/" if it is loading the change view (or maybe just "change" depending on your Django site settings).
def get_queryset(self, request):
qs = super().get_queryset(request)
if request.path.endswith("change/"):
# Update the QuerySet as appropriate here for the change view
qs = qs.select_related("whatever").prefetch_related("whatever")
return qs