Skip to content

Commit

Permalink
Merge pull request #2188 from IFRCGo/feature/fix-event-pk-nplus1
Browse files Browse the repository at this point in the history
Fix event-pk query n+1 selects
  • Loading branch information
szabozoltan69 authored Jul 1, 2024
2 parents 843f5b5 + aef6d58 commit 9c69de1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
11 changes: 8 additions & 3 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,27 @@ 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)
.values("country")
.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
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
22 changes: 20 additions & 2 deletions deployments/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down

0 comments on commit 9c69de1

Please sign in to comment.