Skip to content

Commit

Permalink
Merge pull request #513 from ita-social-projects/#508-InvestigateAndR…
Browse files Browse the repository at this point in the history
…esolveN+1Problem

#508 Investigate and resolve N+1 problem
  • Loading branch information
Lvyshnevska authored Apr 9, 2024
2 parents a5a90cc + 0e70f72 commit a6f98c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
6 changes: 2 additions & 4 deletions administration/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from rest_framework import serializers
from authentication.models import CustomUser
from profiles.models import Profile
from django.contrib.auth import get_user_model
from djoser.serializers import UserSerializer


class AdminUserListSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -47,7 +45,7 @@ class Meta:
"is_startup",
"person",
"person_position",
"region",
"regions",
"official_name",
"phone",
"edrpou",
Expand Down Expand Up @@ -76,7 +74,7 @@ class Meta:
"person",
"person_position",
"official_name",
"region",
"regions",
"common_info",
"phone",
"edrpou",
Expand Down
12 changes: 9 additions & 3 deletions administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UserDetailView(RetrieveUpdateDestroyAPIView):

permission_classes = [IsStaffUser]
serializer_class = AdminUserDetailSerializer
queryset = CustomUser.objects.all()
queryset = CustomUser.objects.select_related("profile")

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
Expand All @@ -63,7 +63,11 @@ class ProfilesListView(ListCreateAPIView):
permission_classes = [IsStaffUser]
pagination_class = ListPagination
serializer_class = AdminCompanyListSerializer
queryset = Profile.objects.all().order_by("id")
queryset = (
Profile.objects.select_related("person")
.prefetch_related("regions", "categories", "activities")
.order_by("id")
)


class ProfileDetailView(RetrieveUpdateDestroyAPIView):
Expand All @@ -73,4 +77,6 @@ class ProfileDetailView(RetrieveUpdateDestroyAPIView):

permission_classes = [IsStaffUser]
serializer_class = AdminCompanyDetailSerializer
queryset = Profile.objects.all()
queryset = Profile.objects.select_related("person").prefetch_related(
"regions", "categories", "activities"
)
21 changes: 16 additions & 5 deletions profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ListCreateAPIView,
DestroyAPIView,
RetrieveUpdateDestroyAPIView,
ListAPIView,
)
from rest_framework.permissions import (
IsAuthenticatedOrReadOnly,
Expand Down Expand Up @@ -122,7 +121,11 @@ def get_serializer_context(self):

def get_queryset(self):
user_id = self.request.query_params.get("userid")
queryset = Profile.objects.active_only().order_by("id")
queryset = (
Profile.objects.active_only()
.prefetch_related("regions", "categories", "activities")
.order_by("id")
)
if user_id:
try:
return queryset.filter(person_id=user_id)
Expand All @@ -146,7 +149,11 @@ class ProfileDetail(RetrieveUpdateDestroyAPIView):
If user is authenticated, he can get sensitive data via query param 'with_contacts'.
"""

queryset = Profile.objects.active_only()
queryset = (
Profile.objects.active_only()
.select_related("person")
.prefetch_related("regions", "categories", "activities")
)
permission_classes = [UserIsProfileOwnerOrReadOnly]

def get_serializer_context(self):
Expand All @@ -164,7 +171,11 @@ def get_serializer_class(self):
get_contacts = self.request.query_params.get("with_contacts")

profile_pk = self.kwargs.get("pk")
profile_instance = Profile.objects.filter(id=profile_pk).first()
profile_instance = (
Profile.objects.select_related("person")
.filter(id=profile_pk)
.first()
)
user_pk = self.request.user.id

if self.request.method == "GET":
Expand Down Expand Up @@ -203,7 +214,7 @@ def current_user(self):
@cached_property
def _profile(self):
return get_object_or_404(
Profile.objects.filter(is_deleted=False),
Profile.objects.active_only(),
pk=self.kwargs["profile_id"],
)

Expand Down
4 changes: 3 additions & 1 deletion search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


class SearchCompanyView(ListAPIView):
queryset = Profile.objects.active_only()
queryset = Profile.objects.active_only().prefetch_related(
"regions", "categories", "activities"
)
serializer_class = CompanySerializers
filter_backends = [
django_filters.rest_framework.DjangoFilterBackend,
Expand Down

0 comments on commit a6f98c9

Please sign in to comment.