diff --git a/api/drf_views.py b/api/drf_views.py index 3172d5e8c..40b14bad4 100644 --- a/api/drf_views.py +++ b/api/drf_views.py @@ -668,9 +668,13 @@ def get_serializer_class(self): def retrieve(self, request, pk=None, *args, **kwargs): if pk: try: + FR = Prefetch( + "field_reports", + queryset=FieldReport.objects.prefetch_related("countries", "contacts"), + ) if self.request.user.is_authenticated: if is_user_ifrc(self.request.user): - instance = Event.objects.get(pk=pk) + instance = Event.objects.prefetch_related(FR).get(pk=pk) else: user_countries = ( UserCountry.objects.filter(user=request.user.id) @@ -678,12 +682,13 @@ def retrieve(self, request, pk=None, *args, **kwargs): .union(Profile.objects.filter(user=request.user.id).values("country")) ) instance = ( - Event.objects.exclude(visibility=VisibilityChoices.IFRC) + Event.objects.prefetch_related(FR) + .exclude(visibility=VisibilityChoices.IFRC) .exclude(Q(visibility=VisibilityChoices.IFRC_NS) & ~Q(countries__id__in=user_countries)) .get(pk=pk) ) else: - instance = Event.objects.filter(visibility=VisibilityChoices.PUBLIC).get(pk=pk) + instance = Event.objects.prefetch_related(FR).filter(visibility=VisibilityChoices.PUBLIC).get(pk=pk) # instance = Event.get_for(request.user).get(pk=pk) except Exception: raise Http404 diff --git a/api/serializers.py b/api/serializers.py index e950931ec..8bce833db 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -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) diff --git a/deployments/drf_views.py b/deployments/drf_views.py index 124b89b1f..4c69e24d9 100644 --- a/deployments/drf_views.py +++ b/deployments/drf_views.py @@ -424,7 +424,15 @@ class ProjectViewset( Project.objects.select_related( "user", "modified_by", "project_country", "reporting_ns", "dtype", "regional_project", "primary_sector" ) - .prefetch_related("project_districts", "event", "annual_splits", "secondary_sectors", "project_admin2") + .prefetch_related( + "project_districts", + "event", + "event__appeals", + "event__countries_for_preview", + "annual_splits", + "secondary_sectors", + "project_admin2", + ) .order_by("-modified_at") .all() ) @@ -840,7 +848,17 @@ class EmergencyProjectViewSet( ): queryset = ( EmergencyProject.objects.select_related("created_by", "reporting_ns", "event", "country", "deployed_eru", "modified_by") - .prefetch_related("districts", "activities", "admin2") + .prefetch_related( + "districts", + "admin2", + "event__appeals", + "event__countries_for_preview", + "activities", + "activities__sector", + "activities__action", + "activities__action__supplies", + "activities__points", + ) .order_by("-modified_at") .all() )