Skip to content

Commit 81b93bb

Browse files
petrprikrylpetr.prikryl
andauthored
optimization of AttachmentInline (#413)
preventing iteration over all attachments in database Co-authored-by: petr.prikryl <[email protected]>
1 parent 1cff212 commit 81b93bb

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

post_office/admin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ def get_message_preview(instance):
3333
class AttachmentInline(admin.StackedInline):
3434
model = Attachment.emails.through
3535
extra = 0
36+
autocomplete_fields = ["attachment"]
37+
38+
def get_formset(self, request, obj=None, **kwargs):
39+
self.parent_obj = obj
40+
return super().get_formset(request, obj, **kwargs)
3641

3742
def get_queryset(self, request):
3843
"""
3944
Exclude inlined attachments from queryset, because they usually have meaningless names and
4045
are displayed anyway.
4146
"""
4247
queryset = super().get_queryset(request)
48+
if self.parent_obj:
49+
queryset = queryset.filter(email=self.parent_obj)
50+
4351
inlined_attachments = [
4452
a.id
4553
for a in queryset
@@ -314,6 +322,7 @@ def save_model(self, request, obj, form, change):
314322
class AttachmentAdmin(admin.ModelAdmin):
315323
list_display = ['name', 'file']
316324
filter_horizontal = ['emails']
325+
search_fields = ["name"]
317326

318327

319328
admin.site.register(Email, EmailAdmin)

0 commit comments

Comments
 (0)