Skip to content

Commit 95dd99a

Browse files
Merge pull request #90 from onyx-flame/bugfix/excess-sql-queries
N+1 SQL query problem fix
2 parents 284421c + 3ca400c commit 95dd99a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

django_admin_index/models.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.contrib.admin import site
22
from django.contrib.contenttypes.models import ContentType
33
from django.db import models
4-
from django.db.models import F
4+
from django.db.models import F, Prefetch
55
from django.urls import reverse
66
from django.utils.text import capfirst
77
from django.utils.translation import get_language, gettext_lazy as _
@@ -17,7 +17,7 @@ def get_by_natural_key(self, slug):
1717
return self.get(slug=slug)
1818

1919
def as_list(self, request, include_remaining=True):
20-
# Convert to convienent dict
20+
# Convert to convenient dict
2121
model_dicts = {}
2222

2323
original_app_list = site.get_app_list(request)
@@ -56,8 +56,16 @@ def as_list(self, request, include_remaining=True):
5656
# Create new list based on our groups, using the model_dicts constructed above. # noqa
5757
result = []
5858
app_list = self.annotate(
59-
localized_name=F(f"translations__{language_code}")
60-
).prefetch_related("models", "applink_set")
59+
localized_name=F(f"translations__{language_code}"),
60+
).prefetch_related(
61+
"models",
62+
Prefetch(
63+
"applink_set",
64+
queryset=AppLink.objects.annotate(
65+
localized_name=F(f"translations__{language_code}"),
66+
),
67+
),
68+
)
6169
active_app = request.path == reverse("admin:index")
6270
for app in app_list:
6371
models = []
@@ -71,9 +79,7 @@ def as_list(self, request, include_remaining=True):
7179
if o["active"]:
7280
active = True
7381

74-
for app_link in app.applink_set.annotate(
75-
localized_name=F(f"translations__{language_code}")
76-
):
82+
for app_link in app.applink_set.all():
7783
models.append(
7884
{
7985
"name": app_link.localized_name or app_link.name,

0 commit comments

Comments
 (0)