Attempt to reduce the number of database calls for user_api serialization

This commit is contained in:
Calen Pennington
2017-04-12 15:38:00 -04:00
parent 0c82bae91c
commit 4cc23bc641
2 changed files with 4 additions and 9 deletions

View File

@@ -22,12 +22,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
"""
Return the name attribute from the user profile object if profile exists else none
"""
try:
profile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
return None
return profile.name
return user.profile.name
def get_preferences(self, user):
"""

View File

@@ -956,7 +956,7 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (ApiKeyHeaderPermission,)
queryset = User.objects.all().prefetch_related("preferences")
queryset = User.objects.all().prefetch_related("preferences").select_related("profile")
serializer_class = UserSerializer
paginate_by = 10
paginate_by_param = "page_size"
@@ -982,7 +982,7 @@ class ForumRoleUsersListView(generics.ListAPIView):
raise ParseError('course_id must be specified')
course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id_string)
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
@@ -1011,7 +1011,7 @@ class PreferenceUsersListView(generics.ListAPIView):
paginate_by_param = "page_size"
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):