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:
parent
ce0601b77c
commit
d36cb87299
@ -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):
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user