2017-08-16 95 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

很高兴为您效劳) –