Skip to content

Commit

Permalink
Fix performance of EmailAdmin view.
Browse files Browse the repository at this point in the history
Add a `filter` and `select_related` to the queryset to limit the
records being fetched by the custom `get_queryset` logic.

Fixes ui#415
  • Loading branch information
barqshasbite committed Sep 19, 2022
1 parent 59d320d commit 30e22a2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion post_office/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ def get_queryset(self, request):
are displayed anyway.
"""
queryset = super().get_queryset(request)

# The queryset does not have the Email FK filter applied at this point.
# That happens later when the inline formset is created as part of the
# admin view. Here we explicitly add the Email filter to avoid selecting
# every row in the table during this special filtering step.
email = request.resolver_match.kwargs.get("object_id", None)
if email is None:
return queryset
email_filter = queryset.filter(email=email).select_related("attachment")

inlined_attachments = [
a.id
for a in queryset
for a in email_filter
if isinstance(a.attachment.headers, dict)
and a.attachment.headers.get("Content-Disposition", "").startswith("inline")
]
Expand Down

0 comments on commit 30e22a2

Please sign in to comment.