From 6b3353fbe3cc823904b08d185d10565c18598c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Wed, 13 Nov 2024 18:14:01 +0100 Subject: [PATCH] fix N+1 queries and add test to prevent them --- lemarche/www/dashboard_favorites/tests.py | 7 +++++-- lemarche/www/dashboard_favorites/views.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lemarche/www/dashboard_favorites/tests.py b/lemarche/www/dashboard_favorites/tests.py index 286a3ae48..9e39909ea 100644 --- a/lemarche/www/dashboard_favorites/tests.py +++ b/lemarche/www/dashboard_favorites/tests.py @@ -34,5 +34,8 @@ def test_only_favorite_list_user_can_view_favorite_list_detail(self): # favorite list user self.client.force_login(self.user_favorite_list) url = reverse("dashboard_favorites:list_detail", args=[self.favorite_list_1.slug]) - response = self.client.get(url) - self.assertEqual(response.status_code, 200) + + # check number of queries + with self.assertNumQueries(6): + response = self.client.get(url) + self.assertEqual(response.status_code, 200) diff --git a/lemarche/www/dashboard_favorites/views.py b/lemarche/www/dashboard_favorites/views.py index d2c843ba0..294571be8 100644 --- a/lemarche/www/dashboard_favorites/views.py +++ b/lemarche/www/dashboard_favorites/views.py @@ -64,7 +64,7 @@ def get_success_message(self, cleaned_data): class DashboardFavoriteListDetailView(FavoriteListOwnerRequiredMixin, DetailView): template_name = "favorites/dashboard_favorite_list_detail.html" - queryset = FavoriteList.objects.prefetch_related("siaes").all() + queryset = FavoriteList.objects.prefetch_related("siaes", "siaes__activities__sector_group").all() context_object_name = "favorite_list" def get_context_data(self, **kwargs):