Skip to content

Commit

Permalink
Merge pull request #90 from onyx-flame/bugfix/excess-sql-queries
Browse files Browse the repository at this point in the history
N+1 SQL query problem fix
  • Loading branch information
sergei-maertens authored Feb 28, 2024
2 parents 284421c + 3ca400c commit 95dd99a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions django_admin_index/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand All @@ -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)
Expand Down Expand Up @@ -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 = []
Expand All @@ -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,
Expand Down

0 comments on commit 95dd99a

Please sign in to comment.