django 추가기능 답변 정렬 질문입니다

base_view
'''
from typing import Any

from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404
from django.db.models import Q, Count

from ..models import Question, Answer, Category

def index(request):
page = request.GET.get('page', '1') # 페이지
kw = request.GET.get('kw', '') # 검색어
so = request.GET.get('so', 'recent') # 정렬 기준

if so == 'recommend':
    question_list = Question.objects.annotate(
        num_voter=Count('voter')).order_by('-num_voter', '-create_date')

elif so == 'popular':
    question_list = Question.objects.annotate(
        num_answer=Count('answer')).order_by('-num_answer', '-create_date')

else:  # recent
    question_list = Question.objects.order_by('-create_date')

if kw:
    question_list = question_list.filter(
        Q(subject__icontains=kw) |
        Q(content__icontains=kw) |
        Q(author__username__icontains=kw) |
        Q(answer__author__username__icontains=kw)
    ).distinct()

paginator = Paginator(question_list, 10)  # 페이지당 10개씩 보여주기
page_obj = paginator.get_page(page)
context = {'question_list': page_obj, 'page': page, 'kw': kw, 'so': so,
        }
return render(request, 'pybo/question_list.html', context)

def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
page = request.GET.get('page', '1')
so = request.GET.get('so', 'recent') # 정렬 기준

if so == 'recommend':
    answer_list = Answer.objects.annotate(
        num_voter=Count('voter'))(question=question).order_by('-num_voter', '-create_date')

else:  # recent
    answer_list = Answer.objects.filter(question=question).order_by('-create_date')

paginator = Paginator(answer_list, 10)
page_obj = paginator.get_page(page)

context = {'question': question, 'answer_list': page_obj, 'page': page, 'so': so}
return render(request, 'pybo/question_detail.html', context)
    '''

    여기서 추천순과 최신순까지는 고르는 것까지는 성공하였는데 
    추천순을 누르게되면 
    TypeError at /pybo/306/

'QuerySet' object is not callable
Request Method: GET
Request URL: http://localhost:8000/pybo/306/?page=1&so=recommend
Django Version: 3.1.3
Exception Type: TypeError
Exception Value:
'QuerySet' object is not callable
이런 오류가 뜨게됩니다..

ksg3477 52

2022년 6월 22일 12:47 오전

목록으로
1개의 답변이 있습니다. 1 / 1 Page
answer_list = Answer.objects.annotate(
        num_voter=Count('voter'))(question=question).order_by('-num_voter', '-create_date')

answer_list = Answer.objects..filter(question=question).annotate(
        num_voter=Count('voter')).order_by('-num_voter', '-create_date')

처럼 수정하셔야 할듯하네요.

박응용

2022년 6월 23일 1:31 오전