Skip to content

Commit

Permalink
optimization of AttachmentInline (#413)
Browse files Browse the repository at this point in the history
preventing iteration over all attachments in database

Co-authored-by: petr.prikryl <petr.prikryl@olc.cz>
  • Loading branch information
petrprikryl and petr.prikryl authored Oct 7, 2022
1 parent 1cff212 commit 81b93bb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions post_office/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ def get_message_preview(instance):
class AttachmentInline(admin.StackedInline):
model = Attachment.emails.through
extra = 0
autocomplete_fields = ["attachment"]

def get_formset(self, request, obj=None, **kwargs):
self.parent_obj = obj
return super().get_formset(request, obj, **kwargs)

def get_queryset(self, request):
"""
Exclude inlined attachments from queryset, because they usually have meaningless names and
are displayed anyway.
"""
queryset = super().get_queryset(request)
if self.parent_obj:
queryset = queryset.filter(email=self.parent_obj)

inlined_attachments = [
a.id
for a in queryset
Expand Down Expand Up @@ -314,6 +322,7 @@ def save_model(self, request, obj, form, change):
class AttachmentAdmin(admin.ModelAdmin):
list_display = ['name', 'file']
filter_horizontal = ['emails']
search_fields = ["name"]


admin.site.register(Email, EmailAdmin)
Expand Down

0 comments on commit 81b93bb

Please sign in to comment.