Skip to content

Commit ff693fb

Browse files
Merge pull request #2729 from IFRCGo/fix/historical-appeals-n+1-fix
Fix GoHistorical N+1 query issue
2 parents 07cfe89 + f31f3c8 commit ff693fb

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

api/drf_views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,15 @@ class GoHistoricalViewSet(viewsets.ReadOnlyModelViewSet):
14771477
filterset_class = GoHistoricalFilter
14781478

14791479
def get_queryset(self):
1480-
return Event.objects.filter(appeals__isnull=False).distinct()
1480+
return (
1481+
Event.objects.annotate(has_appeals=models.Exists(Appeal.objects.filter(event=OuterRef("pk"))))
1482+
.filter(has_appeals=True)
1483+
.select_related("dtype")
1484+
.prefetch_related(
1485+
Prefetch("countries", queryset=Country.objects.select_related("region")),
1486+
"appeals",
1487+
)
1488+
)
14811489

14821490

14831491
class CountryOfFieldReportToReviewViewset(viewsets.ReadOnlyModelViewSet):

0 commit comments

Comments
 (0)