diff --git a/administration/serializers.py b/administration/serializers.py index 8518f74d4..0875c2400 100644 --- a/administration/serializers.py +++ b/administration/serializers.py @@ -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): @@ -47,7 +45,7 @@ class Meta: "is_startup", "person", "person_position", - "region", + "regions", "official_name", "phone", "edrpou", @@ -76,7 +74,7 @@ class Meta: "person", "person_position", "official_name", - "region", + "regions", "common_info", "phone", "edrpou", diff --git a/administration/views.py b/administration/views.py index 74f317186..7b36c378f 100644 --- a/administration/views.py +++ b/administration/views.py @@ -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() @@ -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): @@ -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" + ) diff --git a/profiles/views.py b/profiles/views.py index ef87ea139..70246aed0 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -7,7 +7,6 @@ ListCreateAPIView, DestroyAPIView, RetrieveUpdateDestroyAPIView, - ListAPIView, ) from rest_framework.permissions import ( IsAuthenticatedOrReadOnly, @@ -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) @@ -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): @@ -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": @@ -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"], ) diff --git a/search/views.py b/search/views.py index 781bc421a..5af58703a 100644 --- a/search/views.py +++ b/search/views.py @@ -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,