Django docs Polls App, Diferent queryset returns same object values but QuestionIndexViewTests works for one query and fails for other

瀏覽次數:8 次
跳到第一則未讀訊息

Himanshu Gamit

未讀,
2018年4月13日 晚上9:14:482018/4/13
收件者:Django users
In polls application I added annotation to check one to many relationship between Question and Choice models such that, Index only return questions with more than one choices to be displayed but my testcase fails all the time with annotation where both query returns same data.
views.py (Not Working: Added `annotate(num = Count('choice')).filter(num__gt=1))`
{{{
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'
    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.annotate(num = Count('choice')).filter(num__gt=1).filter(pub___lte=timezone.now()).order_by('-pub_date')[:5]
}}}
Error:
{{{
Traceback (most recent call last): File "C:\Users\hmnsh\repos\DjangoApp\mysite\polls\tests.py", line 80, in test_past_question ['']
File "C:\ProgramData\Anaconda3\envs\django\lib\site-packages\django\test\testcases.py", line 940, in assertQuerysetEqual return self.assertEqual(list(items), values, msg=msg) AssertionError: Lists differ: [] != ['']
}}}

Please help.
回覆所有人
回覆作者
轉寄
0 則新訊息