From d56c505c6eb4d154c4c7addc7b61c87b40c94d6b Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 28 Mar 2023 12:49:42 -0400 Subject: [PATCH] rentals: Improve performance of Locker Info admin --- rentals/admin.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rentals/admin.py b/rentals/admin.py index 5fed375..9fd1568 100644 --- a/rentals/admin.py +++ b/rentals/admin.py @@ -1,6 +1,8 @@ +from django.db.models import Prefetch from django.contrib import admin from django import forms +from membershipworks.models import Member from .models import LockerBank, LockerInfo, LockerUnit @@ -28,7 +30,7 @@ class LockerUnitAdmin(admin.ModelAdmin): @admin.register(LockerInfo) class LockerInfoAdmin(admin.ModelAdmin): search_fields = ["renter__username", "locker_unit__bank__name"] - list_filter = ["locker_unit__bank", "locker_unit", "renter"] + list_filter = ["locker_unit__bank", "locker_unit"] list_display = [ "locker_unit", "address", @@ -41,7 +43,11 @@ class LockerInfoAdmin(admin.ModelAdmin): list_editable = ["blind_code", "bitting_code"] def get_queryset(self, request): - return LockerInfo.objects.select_related("locker_unit", "locker_unit__bank") + return LockerInfo.objects.select_related( + "locker_unit", "locker_unit__bank" + ).prefetch_related( + Prefetch("renter", queryset=Member.objects.only("account_name")) + ) def get_changelist_formset(self, request, **kwargs): kwargs["widgets"] = {