2014-11-09 43 views
0

我一直在努力通過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] 

它只是使用的過濾器()函數來過濾掉在將來,這其實很簡單一個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()方法REF方式基於他們是否有選擇進行民意測驗?

+1

HTTP運行測試至少創建一個選擇的get_queryset:// stackoverflow.com/a/844572/3033586 – madzohan 2014-11-09 11:25:48

回答

1
def show_polls_with_choices(self):  
    return Poll.objects.exlude(choice_set__isnull=True) 


def get_queryset(self): 
    """Return the last five published NOT EMPTY polls.""" 
    #Returns polls published at a date the same as now or before 
    return self.show_polls_with_choices().filter(pub_date__lte=timezone.now()).order_by('pub_date')[:5] 
+0

我仍然得到同樣的錯誤,民意調查正在發佈。你看到我的測試代碼有什麼問題嗎? – Amon 2014-11-09 22:27:52

+1

它說'['','<輪詢:...!= ['<輪詢:全部輪詢>']'一個列表!=另一個列表,第一個從'get_queryset'它是'['','']',並且您在測試中手動聲明的第二個'['']' – madzohan 2014-11-09 23:22:49

+1

你只想得到'['']'你必須編輯'get_queryset'類似於'return self.show_polls_with_choices()。filter(pub_date__lte = timezone.now())。order_by('pub_date ')' – madzohan 2014-11-09 23:28:19

1
class IndexView(generic.ListView): 
template_name = 'polls/index.html' 
context_object_name = 'latest_question_list' 

def get_queryset(self): 
    return Polls.objects.exclude(choice__isnull=True).filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:10] 

這可能是因爲它結合瞭解決問題的最簡單的方法;你的功能,這是必要的,以便工作

,如果它用於IndexView你需要在你的索引視圖

+0

你可以編輯你的答案,而不是評論它的解釋。 – 2015-07-28 21:53:15