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)
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:

View File

@ -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):

View File

@ -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):