From ab130a08ed6a3f18c8afec9f86032be605f470b9 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 9 Feb 2026 12:13:00 +0000 Subject: [PATCH] Optimize collection_viva by annotating and prefetching related objects to prevent N+1 queries --- atlas/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/atlas/views.py b/atlas/views.py index 1b7d45b3..938d814d 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -3127,6 +3127,17 @@ def collection_viva(request, pk): ) ) + # Also annotate and prefetch additional related objects used by the viva + # template to avoid N+1 queries when rendering many cases. + casedetails = ( + casedetails + .annotate(series_count=Count("case__seriesdetail")) + .prefetch_related( + "case__caseresource_set", + "case__display_sets", + ) + ) + return render( request,