我正在通过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中的一切都像预期的那样工作。
知道问题出在哪里吗?我是一个有php背景和一些OOP经验的python新手。
提前谢谢你,
帐单
发布于 2013-02-07 03:35:47
是否确实要直接从教程复制。看起来不像是choice_text=,而是choice=
# 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模型,并接受poll - p -并将其指定为模型字段poll (外键)。然后将choice=值分配给模型字段choice,并将votes=值分配给模型字段votes。
https://stackoverflow.com/questions/14737257
复制相似问题