From d36cb872991f9acce2315e4f79bfa0e74159a59b Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Wed, 13 Nov 2024 16:21:13 +0000 Subject: [PATCH] Use values_list() to optimise tag autocomplete view --- wagtail/admin/tests/tests.py | 2 ++ wagtail/admin/views/tags.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/wagtail/admin/tests/tests.py b/wagtail/admin/tests/tests.py index a2a62b6ab9..818b3c3b1b 100644 --- a/wagtail/admin/tests/tests.py +++ b/wagtail/admin/tests/tests.py @@ -391,6 +391,8 @@ class TestTagsAutocomplete(WagtailTestUtils, TestCase): # The results should be limited to avoid performance issues (#12415) self.assertEqual(len(data), 10) + sorted_tags = sorted(tags, key=lambda t: t.name) + self.assertEqual(data, [tag.name for tag in sorted_tags[:10]]) class TestMenuItem(WagtailTestUtils, TestCase): diff --git a/wagtail/admin/views/tags.py b/wagtail/admin/views/tags.py index f00e5bf889..f4963718f0 100644 --- a/wagtail/admin/views/tags.py +++ b/wagtail/admin/views/tags.py @@ -19,8 +19,12 @@ def autocomplete(request, app_name=None, model_name=None): term = request.GET.get("term", None) if term: - tags = tag_model.objects.filter(name__istartswith=term).order_by("name")[:10] + tags = ( + tag_model.objects.filter(name__istartswith=term) + .order_by("name") + .values_list("name", flat=True)[:10] + ) else: tags = tag_model.objects.none() - return JsonResponse([tag.name for tag in tags], safe=False) + return JsonResponse(list(tags), safe=False)