Limit autocomplete results view to 10

Prevent performance issues when there are many tags available.
pull/12564/head
Aayushman Singh 2024-11-11 11:50:32 +05:30 zatwierdzone przez Sage Abdullah
rodzic 5212061485
commit ce0601b77c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
2 zmienionych plików z 19 dodań i 1 usunięć

Wyświetl plik

@ -374,6 +374,24 @@ class TestTagsAutocomplete(WagtailTestUtils, TestCase):
self.assertEqual(data, [])
def test_tags_autocomplete_limit(self):
tags = [Tag(name=f"Tag {i}", slug=f"tag-{i}") for i in range(15)]
Tag.objects.bulk_create(tags)
# Send a request to the autocomplete endpoint with a broad search term
response = self.client.get(
reverse("wagtailadmin_tag_autocomplete"), {"term": "Tag"}
)
# Confirm the response is successful
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
data = json.loads(response.content.decode("utf-8"))
# The results should be limited to avoid performance issues (#12415)
self.assertEqual(len(data), 10)
class TestMenuItem(WagtailTestUtils, TestCase):
def setUp(self):

Wyświetl plik

@ -19,7 +19,7 @@ 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")
tags = tag_model.objects.filter(name__istartswith=term).order_by("name")[:10]
else:
tags = tag_model.objects.none()