Python Django docs轮询应用程序,不同的queryset返回相同的对象值,但QuestionIndexViewTests对一个查询有效,而对另一个查询失败

Python Django docs轮询应用程序,不同的queryset返回相同的对象值,但QuestionIndexViewTests对一个查询有效,而对另一个查询失败,python,django,Python,Django,在民意测验应用程序中,我添加了注释来检查问题和选择模型之间的一对多关系,这样,索引只返回要显示多个选择的问题,但我的测试用例在注释中始终失败,其中两个查询返回相同的数据 views.py(工作) views.py(不工作:添加.注释(num=Count('choice')).filter(num_ugt=1)) tests.py class QuestionIndexViewTests(TestCase): def test_no_questions(self): """ If

在民意测验应用程序中,我添加了注释来检查问题和选择模型之间的一对多关系,这样,索引只返回要显示多个选择的问题,但我的测试用例在注释中始终失败,其中两个查询返回相同的数据

views.py(工作)

views.py(不工作:添加.注释(num=Count('choice')).filter(num_ugt=1))

tests.py

class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
    """
    If no questions exist, an appropriate message is displayed.
    """
    response = self.client.get(reverse('polls:index'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_past_question(self):
    """
    Questions with a pub_date in the past are displayed on the
    index page.
    """
    create_question(question_text="Past question.", days=-30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_future_question(self):
    """
    Questions with a pub_date in the future aren't displayed on
    the index page.
    """
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_future_question_and_past_question(self):
    """
    Even if both past and future questions exist, only past questions
    are displayed.
    """
    create_question(question_text="Past question.", days=-30)
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_two_past_questions(self):
    """
    The questions index page may display multiple questions.
    """
    create_question(question_text="Past question 1.", days=-30)
    create_question(question_text="Past question 2.", days=-5)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question 2.>', '<Question: Past question 1.>']
    )
类问题索引测试(测试用例):
def测试无问题(自我):
"""
如果不存在任何问题,将显示相应的消息。
"""
response=self.client.get(反向('polls:index'))
self.assertEqual(response.status_代码,200)
self.assertContains(响应,“没有可用的轮询”)
self.assertquerystequal(response.context['latest\u question\u list'],[])
def测试过去问题(自我):
"""
发布日期在过去的问题显示在
索引页。
"""
创建问题(问题文本=“过去的问题”,天数=-30)
response=self.client.get(反向('polls:index'))
self.assertquerystequal(
响应.上下文[“最新问题列表”],
['']
)
def测试未来问题(自我):
"""
带有未来发布日期的问题不会显示在屏幕上
索引页。
"""
创建问题(问题文本=“未来问题”,天数=30)
response=self.client.get(反向('polls:index'))
self.assertContains(响应,“没有可用的轮询”)
self.assertquerystequal(response.context['latest\u question\u list'],[])
def测试未来问题和过去问题(自我):
"""
即使过去和未来的问题都存在,也只有过去的问题
显示。
"""
创建问题(问题文本=“过去的问题”,天数=-30)
创建问题(问题文本=“未来问题”,天数=30)
response=self.client.get(反向('polls:index'))
self.assertquerystequal(
响应.上下文[“最新问题列表”],
['']
)
def测试两个过去的问题(自我):
"""
“问题索引”页面可能会显示多个问题。
"""
创建问题(问题文本=“过去的问题1.”,天数=-30)
创建问题(问题文本=“过去的问题2.”,天数=-5)
response=self.client.get(反向('polls:index'))
self.assertquerystequal(
响应.上下文[“最新问题列表”],
['', '']
)
错误:

Traceback (most recent call last):
  File "C:\Users\hmnsh\repos\DjangoApp\mysite\polls\tests.py", line 80, in test_past_question
    ['<Question: 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: [] != ['<Question: Past question.>']
回溯(最近一次呼叫最后一次):
文件“C:\Users\hmnsh\repos\DjangoApp\mysite\polls\tests.py”,第80行,在测试问题中
['']
文件“C:\ProgramData\Anaconda3\envs\django\lib\site packages\django\test\testcases.py”,第940行,位于AssertQuerySetQual中
返回self.assertEqual(列表(项),值,msg=msg)
断言错误:列表不同:[]!=['']

其中的测试是第940行@GeancarloMurillo,它指的是Django包:Django\lib\site packages\Django\test\testcases.py”,第940行
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
    """
    If no questions exist, an appropriate message is displayed.
    """
    response = self.client.get(reverse('polls:index'))
    self.assertEqual(response.status_code, 200)
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_past_question(self):
    """
    Questions with a pub_date in the past are displayed on the
    index page.
    """
    create_question(question_text="Past question.", days=-30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_future_question(self):
    """
    Questions with a pub_date in the future aren't displayed on
    the index page.
    """
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertContains(response, "No polls are available.")
    self.assertQuerysetEqual(response.context['latest_question_list'], [])

def test_future_question_and_past_question(self):
    """
    Even if both past and future questions exist, only past questions
    are displayed.
    """
    create_question(question_text="Past question.", days=-30)
    create_question(question_text="Future question.", days=30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question.>']
    )

def test_two_past_questions(self):
    """
    The questions index page may display multiple questions.
    """
    create_question(question_text="Past question 1.", days=-30)
    create_question(question_text="Past question 2.", days=-5)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
        response.context['latest_question_list'],
        ['<Question: Past question 2.>', '<Question: Past question 1.>']
    )
Traceback (most recent call last):
  File "C:\Users\hmnsh\repos\DjangoApp\mysite\polls\tests.py", line 80, in test_past_question
    ['<Question: 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: [] != ['<Question: Past question.>']