0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-21 18:09:02 +01:00

Use values_list() to optimise tag autocomplete view

This commit is contained in:
Sage Abdullah 2024-11-13 16:21:13 +00:00
parent ce0601b77c
commit d36cb87299
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
2 changed files with 8 additions and 2 deletions

View File

@ -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):

View File

@ -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)