Skip to content

Commit

Permalink
Fix event-pk query n+1 selects
Browse files Browse the repository at this point in the history
  • Loading branch information
szabozoltan69 committed Jun 29, 2024
1 parent 843f5b5 commit ae67870
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,12 @@ def retrieve(self, request, pk=None, *args, **kwargs):
try:
if self.request.user.is_authenticated:
if is_user_ifrc(self.request.user):
instance = Event.objects.get(pk=pk)
instance = Event.objects.prefetch_related(
Prefetch(
"field_reports",
queryset=FieldReport.objects.prefetch_related("countries", "contacts"),
)
).get(pk=pk)
else:
user_countries = (
UserCountry.objects.filter(user=request.user.id)
Expand Down
4 changes: 2 additions & 2 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ class Meta:


class MiniFieldReportSerializer(ModelSerializer):
contacts = FieldReportContactSerializer(many=True)
countries = MiniCountrySerializer(many=True)
contacts = FieldReportContactSerializer(many=True, read_only=True)
countries = MiniCountrySerializer(many=True, read_only=True)
epi_figures_source_display = serializers.CharField(source="get_epi_figures_source_display", read_only=True)
visibility_display = serializers.CharField(source="get_visibility_display", read_only=True)

Expand Down

0 comments on commit ae67870

Please sign in to comment.