Skip to content

Commit

Permalink
fix: 이름 순 정렬 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kynzun committed Dec 3, 2021
1 parent 2a301cd commit d9c187a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
path("signin", AuthView.as_view()),
path("signup", RegisterAPIView.as_view()),
path("candidate", CandidateListAPIView.as_view()),
path("candidate/result", CandidateResultAPIView.as_view()),
path("candidate/<int:pk>", CandidateDetailAPIView.as_view()),
path("test", TestAPIView.as_view()),
path("testauth", TestAuthAPIView.as_view()),
Expand Down
17 changes: 15 additions & 2 deletions account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,28 @@ def post(self, request):


class CandidateListAPIView(APIView):
def get(self, request, format=None):
candidates = Candidate.objects.all()
part = request.query_params.get("part", None)
if part is not None:
candidates = candidates.filter(part=part)

# 이름순 정렬
candidates = candidates.order_by("name")
serializer = CandidateSerializer(candidates, many=True)
return Response(serializer.data, status.HTTP_200_OK)


class CandidateResultAPIView(APIView):
def get(self, request, format=None):
candidates = Candidate.objects.all()
part = request.GET.get("part", None)
if part is not None:
candidates = candidates.filter(part=part)

# 득표순으로 정렬
# 득표순, 이름순으로 정렬
candidates = candidates.annotate(vote_count=Count("cand_votes")).order_by(
"-vote_count"
"-vote_count", "name"
)
serializer = CandidateSerializer(candidates, many=True)
return Response(serializer.data, status.HTTP_200_OK)
Expand Down

0 comments on commit d9c187a

Please sign in to comment.