프로젝트/I들 ChangeArt
머신러닝 프로젝트 - 백엔드 데이터 정렬하기
sdoram
2023. 5. 27. 23:46
1.annotate()와 order_by
문제점
article에 존재하지 않는 필드의 Count를 기준으로 정렬해야하는 문제 발생
시도해 본 것들
https://sdoram.tistory.com/163
해결 방법
def get(self, request):
current_order = request.query_params.get("order", None)
if current_order == "outdated":
articles = Article.objects.order_by("created_at")
elif current_order == "likes":
articles = Article.objects.annotate(likes_count=Count("like")).order_by(
"-likes_count"
)
elif current_order == "comments":
articles = Article.objects.annotate(
comments_count=Count("comment")
).order_by("-comments_count")
elif current_order is None:
articles = Article.objects.order_by("-created_at")
이것도 serailizer로 만들어서 view에 존재하는 코드를 최소화 할 수 있는지 생각해보기
알게 된 점
정렬을 위한 데이터는 serializer와 관계가 없음을 알 수 있었다.