TypeError:"choice_text"是Django教程中此函数的无效关键字参数 [英] TypeError: 'choice_text' is an invalid keyword argument for this function in django tutorial

查看:61
本文介绍了TypeError:"choice_text"是Django教程中此函数的无效关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在本教程的结尾部分是有关django DB api的部分,内容如下:

Towards the end of the tutorial is a section on the django DB api there is the following:

# 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'',票数= 0)时,我得到:

However when I directly copy : >>> p.choice_set.create(choice_text='Not much', votes=0) from the tutorial, I get:

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中的所有内容都按预期工作.

previously everything in the tut worked as expected.

任何想法是什么问题?我是来自php并具有OOP经验的Python新手.

Any idea what the problem is ? I'm pretty new to python coming from a php background with some OOP experience.

预先感谢

账单

推荐答案

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

Are you sure you are copying directly from the tutorial. It looks like it is choice= instead of 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()(

So what this line is doing is by using choice_set.create() (link to docs), it's creating a Choice model and taking the poll - p - and assigning that as the model field poll (the foreign key). And then assigning the choice= value to the model field choice, and the votes= value to the model field votes.

这篇关于TypeError:"choice_text"是Django教程中此函数的无效关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆