Skip to content

Commit

Permalink
fix: n+1 query on users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Mar 18, 2024
1 parent 6bb7e50 commit b667e2e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 15 deletions.
3 changes: 2 additions & 1 deletion apps/training/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def get(self, request):
"user": BasicUserSerializer(requests[0].user).data,
"requests": BaseTrainingRequestSerializer(requests, many=True).data,
"last_session": requests[0].last_session,
} for requests in sorted_requests.values()
}
for requests in sorted_requests.values()
]
)

Expand Down
1 change: 0 additions & 1 deletion apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
path("<int:cid>/", views.UserInstanceView.as_view()),
path("<int:cid>/feedback/", views.UserFeedbackView.as_view()),
path("simplified/", views.SimplifiedActiveUserListView.as_view()),
path("all/", views.AllUserListView.as_view()),
path("newest/", views.NewestUserListView.as_view()),
path("staff/", views.StaffListView.as_view()),
]
14 changes: 1 addition & 13 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get(self, request):
Get list of all active users sorted by first name.
Sorted into home and visiting controllers.
"""
users = User.objects.filter(status=Status.ACTIVE).order_by("first_name")
users = User.objects.filter(status=Status.ACTIVE).prefetch_related("roles").order_by("first_name")
if request.user.is_authenticated and request.user.is_staff:
serializer = AuthenticatedUserSerializer
else:
Expand Down Expand Up @@ -142,18 +142,6 @@ def get(self, request):
)


class AllUserListView(APIView):
permission_classes = [IsStaff]

def get(self, request):
"""
Get list of all users sorted by first name.
"""
users = User.objects.order_by("first_name")
serializer = AuthenticatedUserSerializer(users, many=True)
return Response(serializer.data)


class NewestUserListView(APIView):
def get(self, request):
"""
Expand Down

0 comments on commit b667e2e

Please sign in to comment.