본문 바로가기

Django

Queryset 역순으로 정렬하기 (How to reverse the order of query result)

반응형

order_by 함수를 이용할 때 Column ID 앞에 - 를 붙이면 역순으로 정렬 된다.

def post_detail(request, post_id):
    post = Post.objects.get(pk=post_id)
    comment = Comment.objects.filter(post__id=post_id).order_by('-id')
    return render(request, "HelloWorld/post_detail.html", {'post': post, 'comments':comment})
반응형