Python 尝试将非有序queryset与多个有序值django进行比较

Python 尝试将非有序queryset与多个有序值django进行比较,python,django,unit-testing,django-queryset,Python,Django,Unit Testing,Django Queryset,此单元测试失败,出现以下异常: def test_vote_form_with_multiple_choices_allowed_and_submitted(self): """ If multiple choices are allowed and submitted, the form should be valid. """ vote_form = VoteForm({'choice': [1, 2]}, instance=create_question('

此单元测试失败,出现以下异常:

def test_vote_form_with_multiple_choices_allowed_and_submitted(self):
    """
    If multiple choices are allowed and submitted, the form should be valid.
    """
    vote_form = VoteForm({'choice': [1, 2]}, instance=create_question('Dummy question', -1,
                                                                      [Choice(choice_text='First choice'), Choice(
                                                                          choice_text='Second choice')],
                                                                      allow_multiple_choices=True))
    self.assertTrue(vote_form.is_valid())
    self.assertQuerysetEqual(vote_form.cleaned_data['choice'], ['<Choice: First choice>', '<Choice: Second choice>'])
def test_vote_form_,允许选择多个选项,并提交(self):
"""
如果允许并提交多项选择,则表格应有效。
"""
vote_form=VoteForm({'choice':[1,2]},instance=create_question('Dummy question'),-1,
[Choice(Choice_text='First Choice'),Choice(
选项[u text='Second choice')],
允许(多个(选择=真))
self.assertTrue(投票形式是否有效()
self.assertQuerysetEqual(投票表格清理数据['choice'],['',])
ValueError:尝试将非有序queryset与多个有序值进行比较

我做错了什么?

来自:

默认情况下,比较也取决于顺序。如果qs不提供隐式排序,则可以将ordered参数设置为False,从而将比较转换为collections.Counter比较。如果顺序未定义(如果给定的qs未排序,并且与多个排序值进行比较),则会引发ValueError

您正在将
查询集
列表
进行比较。列表有顺序,但Queryset没有

因此,您可以将QuerySet转换为list

queryset = vote_form.cleaned_data['choice']
self.assertQuerysetEqual(list(queryset), ['<Choice: First choice>', ...])
在比较之前对Queryset重新排序也应该有效

queryset = vote_form.cleaned_data['choice']
self.assertQuerysetEqual(list(queryset), ['<Choice: First choice>', ...], ordered=False)