Skip to content

Commit

Permalink
fix N+1 queries and add test to prevent them
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Nov 13, 2024
1 parent a8cb26c commit 6b3353f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lemarche/www/dashboard_favorites/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion lemarche/www/dashboard_favorites/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 6b3353f

Please sign in to comment.