Commit 4cc23bc6 by Calen Pennington

Attempt to reduce the number of database calls for user_api serialization

parent 0c82bae9
...@@ -22,12 +22,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): ...@@ -22,12 +22,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
""" """
Return the name attribute from the user profile object if profile exists else none Return the name attribute from the user profile object if profile exists else none
""" """
try: return user.profile.name
profile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
return None
return profile.name
def get_preferences(self, user): def get_preferences(self, user):
""" """
......
...@@ -956,7 +956,7 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -956,7 +956,7 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet):
""" """
authentication_classes = (authentication.SessionAuthentication,) authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (ApiKeyHeaderPermission,) permission_classes = (ApiKeyHeaderPermission,)
queryset = User.objects.all().prefetch_related("preferences") queryset = User.objects.all().prefetch_related("preferences").select_related("profile")
serializer_class = UserSerializer serializer_class = UserSerializer
paginate_by = 10 paginate_by = 10
paginate_by_param = "page_size" paginate_by_param = "page_size"
...@@ -982,7 +982,7 @@ class ForumRoleUsersListView(generics.ListAPIView): ...@@ -982,7 +982,7 @@ class ForumRoleUsersListView(generics.ListAPIView):
raise ParseError('course_id must be specified') raise ParseError('course_id must be specified')
course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id_string) course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id_string)
role = Role.objects.get_or_create(course_id=course_id, name=name)[0] role = Role.objects.get_or_create(course_id=course_id, name=name)[0]
users = role.users.all() users = role.users.prefetch_related("preferences").select_related("profile").all()
return users return users
...@@ -1011,7 +1011,7 @@ class PreferenceUsersListView(generics.ListAPIView): ...@@ -1011,7 +1011,7 @@ class PreferenceUsersListView(generics.ListAPIView):
paginate_by_param = "page_size" paginate_by_param = "page_size"
def get_queryset(self): def get_queryset(self):
return User.objects.filter(preferences__key=self.kwargs["pref_key"]).prefetch_related("preferences") return User.objects.filter(preferences__key=self.kwargs["pref_key"]).prefetch_related("preferences").select_related("profile")
class UpdateEmailOptInPreference(APIView): class UpdateEmailOptInPreference(APIView):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment