From 3ca400ceb2e5e715d5a8257c259c87f2476e9784 Mon Sep 17 00:00:00 2001 From: onyx-flame Date: Mon, 25 Dec 2023 23:00:55 +0300 Subject: [PATCH] fix: got rid of excess SQL queries --- django_admin_index/models.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/django_admin_index/models.py b/django_admin_index/models.py index e05ac23..00b4ac3 100644 --- a/django_admin_index/models.py +++ b/django_admin_index/models.py @@ -1,7 +1,7 @@ from django.contrib.admin import site from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models import F +from django.db.models import F, Prefetch from django.urls import reverse from django.utils.text import capfirst from django.utils.translation import get_language, gettext_lazy as _ @@ -17,7 +17,7 @@ def get_by_natural_key(self, slug): return self.get(slug=slug) def as_list(self, request, include_remaining=True): - # Convert to convienent dict + # Convert to convenient dict model_dicts = {} original_app_list = site.get_app_list(request) @@ -56,8 +56,16 @@ def as_list(self, request, include_remaining=True): # Create new list based on our groups, using the model_dicts constructed above. # noqa result = [] app_list = self.annotate( - localized_name=F(f"translations__{language_code}") - ).prefetch_related("models", "applink_set") + localized_name=F(f"translations__{language_code}"), + ).prefetch_related( + "models", + Prefetch( + "applink_set", + queryset=AppLink.objects.annotate( + localized_name=F(f"translations__{language_code}"), + ), + ), + ) active_app = request.path == reverse("admin:index") for app in app_list: models = [] @@ -71,9 +79,7 @@ def as_list(self, request, include_remaining=True): if o["active"]: active = True - for app_link in app.applink_set.annotate( - localized_name=F(f"translations__{language_code}") - ): + for app_link in app.applink_set.all(): models.append( { "name": app_link.localized_name or app_link.name,