rentals: Improve performance of Locker Info admin

This commit is contained in:
Adam Goldsmith 2023-03-28 12:49:42 -04:00
parent 1cf3f2dcbf
commit d56c505c6e

View File

@ -1,6 +1,8 @@
from django.db.models import Prefetch
from django.contrib import admin from django.contrib import admin
from django import forms from django import forms
from membershipworks.models import Member
from .models import LockerBank, LockerInfo, LockerUnit from .models import LockerBank, LockerInfo, LockerUnit
@ -28,7 +30,7 @@ class LockerUnitAdmin(admin.ModelAdmin):
@admin.register(LockerInfo) @admin.register(LockerInfo)
class LockerInfoAdmin(admin.ModelAdmin): class LockerInfoAdmin(admin.ModelAdmin):
search_fields = ["renter__username", "locker_unit__bank__name"] 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 = [ list_display = [
"locker_unit", "locker_unit",
"address", "address",
@ -41,7 +43,11 @@ class LockerInfoAdmin(admin.ModelAdmin):
list_editable = ["blind_code", "bitting_code"] list_editable = ["blind_code", "bitting_code"]
def get_queryset(self, request): 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): def get_changelist_formset(self, request, **kwargs):
kwargs["widgets"] = { kwargs["widgets"] = {