Improve various admin pages performance using select/prefetch related

This commit is contained in:
Adam Goldsmith 2024-08-07 13:01:28 -04:00
parent d792efc084
commit e11e12307a
3 changed files with 21 additions and 8 deletions

View File

@ -96,6 +96,7 @@ class FlagAdmin(BaseMembershipWorksAdmin):
@admin.register(Transaction) @admin.register(Transaction)
class TransactionAdmin(BaseMembershipWorksAdmin): class TransactionAdmin(BaseMembershipWorksAdmin):
list_display = ["timestamp", "member", "name", "type", "sum", "note"] list_display = ["timestamp", "member", "name", "type", "sum", "note"]
list_select_related = ["member"]
list_filter = ["type"] list_filter = ["type"]
show_facets = admin.ShowFacets.ALWAYS show_facets = admin.ShowFacets.ALWAYS
search_fields = ["member", "name", "type", "note"] search_fields = ["member", "name", "type", "note"]
@ -115,6 +116,7 @@ class EventMeetingTimeInline(admin.TabularInline):
class EventInstructorAdmin(admin.ModelAdmin): class EventInstructorAdmin(admin.ModelAdmin):
autocomplete_fields = ["member"] autocomplete_fields = ["member"]
search_fields = ["name", "member__account_name"] search_fields = ["name", "member__account_name"]
list_select_related = ["member"]
@admin.register(EventInvoice) @admin.register(EventInvoice)
@ -130,6 +132,7 @@ class EventInvoiceAdmin(admin.ModelAdmin):
"date_paid", "date_paid",
"amount", "amount",
] ]
list_select_related = ["event__instructor__member"]
list_filter = [ list_filter = [
("date_paid", admin.EmptyFieldListFilter), ("date_paid", admin.EmptyFieldListFilter),
] ]
@ -186,8 +189,7 @@ class EventAdmin(DjangoObjectActions, admin.ModelAdmin):
change_actions = ["fetch_details"] change_actions = ["fetch_details"]
actions = ["fetch_details"] actions = ["fetch_details"]
@property def get_readonly_fields(self, request: HttpRequest, obj: EventExt) -> list[str]:
def readonly_fields(self):
fields = [] fields = []
for field in Event._meta.get_fields(): for field in Event._meta.get_fields():
if field.auto_created or field.many_to_many or not field.concrete: if field.auto_created or field.many_to_many or not field.concrete:

View File

@ -57,6 +57,7 @@ class DepartmentAdmin(admin.ModelAdmin):
"shop_lead_flag", "shop_lead_flag",
"list_reply_to_address", "list_reply_to_address",
] ]
list_select_related = ["shop_lead_flag", "parent"]
class CertificationVersionInline(admin.TabularInline): class CertificationVersionInline(admin.TabularInline):

View File

@ -1,6 +1,7 @@
from django import forms from django import forms
from django.contrib import admin 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 from membershipworks.models import Member
@ -22,11 +23,17 @@ class LockerBankAdmin(admin.ModelAdmin):
inlines = [LockerUnitInline] inlines = [LockerUnitInline]
prepopulated_fields = {"slug": ("name",)} prepopulated_fields = {"slug": ("name",)}
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerBank]:
return super().get_queryset(request).prefetch_related("units")
@admin.register(LockerUnit) @admin.register(LockerUnit)
class LockerUnitAdmin(admin.ModelAdmin): class LockerUnitAdmin(admin.ModelAdmin):
inlines = [LockerInfoInline] inlines = [LockerInfoInline]
def get_queryset(self, request: HttpRequest) -> QuerySet[LockerUnit]:
return super().get_queryset(request).prefetch_related("bank")
@admin.register(LockerInfo) @admin.register(LockerInfo)
class LockerInfoAdmin(admin.ModelAdmin): class LockerInfoAdmin(admin.ModelAdmin):
@ -43,12 +50,15 @@ class LockerInfoAdmin(admin.ModelAdmin):
] ]
list_display_links = ["locker_unit", "address"] list_display_links = ["locker_unit", "address"]
def get_queryset(self, request): def get_queryset(self, request: HttpRequest) -> QuerySet[LockerInfo]:
return LockerInfo.objects.select_related( return (
"locker_unit", "locker_unit__bank" super()
).prefetch_related( .get_queryset(request)
.select_related("locker_unit", "locker_unit__bank")
.prefetch_related(
Prefetch("renter", queryset=Member.objects.only("account_name")) 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"] = {