TypeError:'choice_text'是django教程中此函数的无效关键字参数

时间:2013-02-06 19:33:19

标签: python django

我正在通过https://docs.djangoproject.com/en/1.4/intro/tutorial01/

在教程结束时,有关django DB api的部分内容如下:

# Display any choices from the related object set -- none so far.
>>> p.choice_set.all()
[]

# Create three choices.
>>> p.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>

但是当我直接复制时:&gt;&gt;&gt; p.choice_set.create(choice_text ='Not much',votes = 0)来自教程,我得到:

raise TypeError("'%s' is an invalid keyword argument for this function" % kw
args.keys()[0])
TypeError: 'choice_text' is an invalid keyword argument for this function

以前Tut中的所有东西都按预期工作。

知道问题是什么吗?我对来自具有一些OOP经验的php背景的python很新。

提前致谢,

比尔

1 个答案:

答案 0 :(得分:5)

您确定要直接从教程中复制吗?它看起来像choice=而不是choice_text=

# Create three choices.
>>> p.choice_set.create(choice='Not much', votes=0)
<Choice: Not much>
>>> p.choice_set.create(choice='The sky', votes=0)
<Choice: The sky>

模型是:

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

这一行正在做的是使用choice_set.create()link to docs),它正在创建一个Choice模型并进行投票 - p - 并将其指定为模型字段poll(外键)。然后将choice=值分配给模型字段choice,将votes=值分配给模型字段votes