Skip to content

Commit

Permalink
perf: optimize DB queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Hicham committed Jan 24, 2025
1 parent a65d9c3 commit ab82f5c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cohort/views/cohort_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class Meta:


class CohortResultViewSet(NestedViewSetMixin, UserObjectsRestrictedViewSet):
queryset = CohortResult.objects.select_related('request_query_snapshot__request')
queryset = CohortResult.objects.select_related('dated_measure',
'dated_measure_global',
'request_query_snapshot__request')
serializer_class = CohortResultSerializer
http_method_names = ['get', 'post', 'patch', 'delete']
swagger_tags = ["Cohorts"]
Expand Down
6 changes: 4 additions & 2 deletions cohort/views/request.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.db.models.query import Prefetch
from django.http import QueryDict
from drf_spectacular.utils import extend_schema_view, extend_schema
from rest_framework import status
from rest_framework_extensions.mixins import NestedViewSetMixin

from admin_cohort.tools.cache import cache_response
from cohort.models import Request
from cohort.models import Request, RequestQuerySnapshot as RQS
from cohort.serializers import RequestSerializer, RequestCreateSerializer, RequestPatchSerializer
from cohort.views.shared import UserObjectsRestrictedViewSet

Expand All @@ -19,7 +20,8 @@
destroy=extend_schema(responses={status.HTTP_204_NO_CONTENT: None})
)
class RequestViewSet(NestedViewSetMixin, UserObjectsRestrictedViewSet):
queryset = Request.objects.all()
queryset = Request.objects.prefetch_related(Prefetch(lookup='query_snapshots',
queryset=RQS.objects.prefetch_related('cohort_results')))
serializer_class = RequestSerializer
http_method_names = ["get", "post", "patch", "delete"]
filterset_fields = ('favorite',
Expand Down

0 comments on commit ab82f5c

Please sign in to comment.