From e11e12307afb87dd0931f86e85e607305c8e4cf8 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 7 Aug 2024 13:01:28 -0400 Subject: [PATCH] Improve various admin pages performance using select/prefetch related --- membershipworks/admin.py | 6 ++++-- paperwork/admin.py | 1 + rentals/admin.py | 22 ++++++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/membershipworks/admin.py b/membershipworks/admin.py index f6fed07..70c3a2b 100644 --- a/membershipworks/admin.py +++ b/membershipworks/admin.py @@ -96,6 +96,7 @@ class FlagAdmin(BaseMembershipWorksAdmin): @admin.register(Transaction) class TransactionAdmin(BaseMembershipWorksAdmin): list_display = ["timestamp", "member", "name", "type", "sum", "note"] + list_select_related = ["member"] list_filter = ["type"] show_facets = admin.ShowFacets.ALWAYS search_fields = ["member", "name", "type", "note"] @@ -115,6 +116,7 @@ class EventMeetingTimeInline(admin.TabularInline): class EventInstructorAdmin(admin.ModelAdmin): autocomplete_fields = ["member"] search_fields = ["name", "member__account_name"] + list_select_related = ["member"] @admin.register(EventInvoice) @@ -130,6 +132,7 @@ class EventInvoiceAdmin(admin.ModelAdmin): "date_paid", "amount", ] + list_select_related = ["event__instructor__member"] list_filter = [ ("date_paid", admin.EmptyFieldListFilter), ] @@ -186,8 +189,7 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin): change_actions = ["fetch_details"] actions = ["fetch_details"] - @property - def readonly_fields(self): + def get_readonly_fields(self, request: HttpRequest, obj: EventExt) -> list[str]: fields = [] for field in Event._meta.get_fields(): if field.auto_created or field.many_to_many or not field.concrete: diff --git a/paperwork/admin.py b/paperwork/admin.py index 5e8a324..d71354b 100644 --- a/paperwork/admin.py +++ b/paperwork/admin.py @@ -57,6 +57,7 @@ class DepartmentAdmin(admin.ModelAdmin): "shop_lead_flag", "list_reply_to_address", ] + list_select_related = ["shop_lead_flag", "parent"] class CertificationVersionInline(admin.TabularInline): diff --git a/rentals/admin.py b/rentals/admin.py index a3ae7d6..d98bb58 100644 --- a/rentals/admin.py +++ b/rentals/admin.py @@ -1,6 +1,7 @@ from django import forms from django.contrib import admin -from django.db.models import Prefetch +from django.db.models import Prefetch, QuerySet +from django.http import HttpRequest from membershipworks.models import Member @@ -22,11 +23,17 @@ class LockerBankAdmin(admin.ModelAdmin): inlines = [LockerUnitInline] prepopulated_fields = {"slug": ("name",)} + def get_queryset(self, request: HttpRequest) -> QuerySet[LockerBank]: + return super().get_queryset(request).prefetch_related("units") + @admin.register(LockerUnit) class LockerUnitAdmin(admin.ModelAdmin): inlines = [LockerInfoInline] + def get_queryset(self, request: HttpRequest) -> QuerySet[LockerUnit]: + return super().get_queryset(request).prefetch_related("bank") + @admin.register(LockerInfo) class LockerInfoAdmin(admin.ModelAdmin): @@ -43,11 +50,14 @@ class LockerInfoAdmin(admin.ModelAdmin): ] list_display_links = ["locker_unit", "address"] - def get_queryset(self, request): - return LockerInfo.objects.select_related( - "locker_unit", "locker_unit__bank" - ).prefetch_related( - Prefetch("renter", queryset=Member.objects.only("account_name")) + def get_queryset(self, request: HttpRequest) -> QuerySet[LockerInfo]: + return ( + super() + .get_queryset(request) + .select_related("locker_unit", "locker_unit__bank") + .prefetch_related( + Prefetch("renter", queryset=Member.objects.only("account_name")) + ) ) def get_changelist_formset(self, request, **kwargs):