From 5d056a7699f92c58b38661c8ca35621e9f5fe790 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Wed, 10 Apr 2024 07:42:52 +0200 Subject: [PATCH] boost efficiency of the ModelAdmin --- django_mailbox/admin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 4d489b1e..aec4d000 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -9,6 +9,7 @@ from django.conf import settings from django.contrib import admin +from django.db.models import Count from django.utils.translation import gettext_lazy as _ from django_mailbox.models import MessageAttachment, Message, Mailbox @@ -60,8 +61,11 @@ class MessageAttachmentInline(admin.TabularInline): class MessageAdmin(admin.ModelAdmin): + def get_queryset(self, *args, **kwargs): + return super().get_queryset(*args, **kwargs).annotate(num_attachments=Count('attachements')) + def attachment_count(self, msg): - return msg.attachments.count() + return msg.num_attachments attachment_count.short_description = _('Attachment count')