0

我正在通过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>

但是,当我直接从教程中复制: >>> 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 中的所有内容都按预期工作。

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

提前致谢,

账单

4

1 回答 1

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()链接到文档),它正在创建一个Choice模型并进行投票 -p并将其分配为模型字段poll(外键)。然后将choice=值分配给模型字段choice,并将votes=值分配给模型字段votes

于 2013-02-06T19:35:46.780 回答