⚡️(admin) optimize room view queries by prefetching user access
Use prefetch_related for the room–user access relationship to avoid N+1 queries. select_related cannot be used here since this is a many-to-many relation. This significantly improves performance.
This commit is contained in:
committed by
aleb_the_flash
parent
fe28902b2e
commit
4344dd6e35
@@ -115,6 +115,10 @@ class RoomAdmin(admin.ModelAdmin):
|
||||
list_filter = ["access_level", "created_at"]
|
||||
readonly_fields = ["id", "created_at", "updated_at"]
|
||||
|
||||
def get_queryset(self, request):
|
||||
"""Optimize queries by prefetching related access and user data to avoid N+1 queries."""
|
||||
return super().get_queryset(request).prefetch_related("accesses__user")
|
||||
|
||||
def get_owner(self, obj):
|
||||
"""Return the owner of the room for display in the admin list."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user