【发布时间】:2015-01-05 15:36:44
【问题描述】:
我一直在努力通过Django tutorial,我在第 5 部分,它要求我创建一个测试,看看是否正在发布没有选择的民意调查。对于我们所做的每一个其他测试,比如确保只发布过去(而不是未来)的民意调查,我们只需创建两个民意调查:一个在过去使用 pub_date,一个在未来:
def test_index_view_with_future_poll_and_past_poll(self):
"""
Even if both past and future polls exist, only past polls should be
displayed.
"""
create_poll(question='past poll', days=-30)
create_poll(question='future poll', days=30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_poll_list'],
['<Poll: past poll>'])
对于对应的视图,我们只是简单地添加了以下函数:
def get_queryset(self):
"""Return the last five published poll."""
#Returns polls published at a date the same as now or before
return Poll.objects.filter(pub_date__lte=timezone.now()).order_by('pub_date')[:5]
它只是使用 filter() 函数来过滤掉任何将来带有pub_date 的投票,这真的很简单。但我似乎无法在没有任何选择的情况下对民意调查做同样的事情,这就是我迄今为止对测试功能所做的:
class PollsAndChoices(TestCase):
""" A poll without choices should not be displayed
"""
def test_poll_without_choices(self):
#first make an empty poll to use as a test
empty_poll = create_poll(question='Empty poll', days=-1)
poll = create_poll(question='Full poll',days=-1)
full_poll = poll.choice_set.create(choice_text='Why yes it is!', votes=0)
#create a response object to simulate someone using the site
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(response.context['latest_poll_list'], ['<Poll: Full poll>'])
这就是我对相关视图的看法:
class IndexView(generic.ListView):
#We tell ListView to use our specific template instead of the default just like the rest
template_name = 'polls/index.html'
#We use this variable because the default variable for Django is poll_list
context_object_name = 'latest_poll_list'
def get_queryset(self):
"""Return the last five published poll."""
#Returns polls published at a date the same as now or before
return Poll.objects.filter(pub_date__lte=timezone.now()).order_by('pub_date')[:5]
def show_polls_with_choices(self):
""" Only publish polls that have choices """
#Since choice_set displays a list we can use it to make sure a poll with an empty list
#is not published
for poll in Poll.objects.all():
if poll.choice_set.all() is not []:
return poll
基本上什么都没有发生,测试失败:
Traceback (most recent call last):
File "C:\Users\ELITEBOOK\dropbox\programming\mysite\polls\tests.py", line 125, in test_poll_without_choices
self.assertQuerysetEqual(response.context['latest_poll_list'], ['<Poll: Full poll>'])
File "C:\Users\Elitebook\Dropbox\Programming\virtualenvs\project\lib\site- packages\django\test\testcases.py", line 829
, in assertQuerysetEqual
return self.assertEqual(list(items), values)
AssertionError: Lists differ: ['<Poll: Empty poll>', '<Poll:... != ['<Poll: Full poll>']
First differing element 0:
<Poll: Empty poll>
<Poll: Full poll>
First list contains 1 additional elements.
First extra element 1:
<Poll: Full poll>
- ['<Poll: Empty poll>', '<Poll: Full poll>']
+ ['<Poll: Full poll>']
所以应用仍然在发布空民意调查和完整民意调查,有没有办法使用filter() 方法根据他们是否有选择来细化民意调查?
【问题讨论】:
标签: python django django-models