2017-08-16 96 views
-1

所以我一直在尋找這個帖子,試圖解決我的Django的教程中遇到了一些問題:Django的教程 - choice_set類型錯誤

TypeError: 'choice_text' is an invalid keyword argument for this function in django tutorial

這是我的代碼:

from polls.models import Question, Choice 
q.choice_set.create(choice_text='Not much', votes=0) 
q.choice_set.create(choice='Not much', votes=0) 

我在採用建議的將choice_text改爲choice的建議解決方案後,仍然面臨完全相同的問題 - 即完全相同的錯誤消息,並且Django教程的文檔版本針對Django 1.11(我的版本)。有人知道創建選擇集的正確語法嗎?

謝謝!


補充信息:我的models.py文件定義問題和選擇。

class Question(models.Model): 
    question_text = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published') 

    def __str__(self): 
     return self.question_text 

    def was_published_recently(self): 
     return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 


class Choice(models.Model): 
    question = models.ForeignKey(Question, on_delete=models.CASCADE) 
    choice_texct = models.CharField(max_length=200) 
    votes = models.IntegerField(default = 0) 

    def __str__(self): 
     return self.choice_text 
+0

顯示您的代碼,請 –

+0

,我輸入的代碼是: q.choice_set.create(choice_text = '不多',票= 0)和q.choice_set.create( choice ='Not much',votes = 0) – tlhy

+0

這是你需要展示的模型。並且這樣做是爲了編輯問題,而不是在評論中。 –

回答

1

您在模型中的錯字:

choice_texct = models.CharField(max_length=200) 
#  ^^^^ 

需要更換

choice_text = models.CharField(max_length=200) 
#  ^^^^ 

,不要忘了做遷移,

或者在你的代碼需要

q.choice_set.create(choice_text='Not much', votes=0) 

更換到

q.choice_set.create(choice_texct='Not much', votes=0) 
#       ^^^^^ 
+0

哦,天哪,你是對的!很抱歉,謝謝你的時間! – tlhy

+0

很高興爲您效勞) –