Django错误:获取关键字参数的多个值 [英] Django error: got multiple values for keyword argument
问题描述
__ init __()获得多个值,以获取以下错误:关键字参数'collection_type'
__ init __()
函数(如下所示)完全是这样写的,但是用我的逻辑代替#code
。除此之外,我基本上覆盖了表单(它是一个ModelForm)构造函数。
def __init __(self,collection_type,user =没有,parent = None,* args,** kwargs)
#code
super(self .__ class__,self).__ init __(* args,** kwargs)
此处显示了创建错误的调用:
code> form = CreateCollectionForm(
request.POST,
collection_type = collection_type,
parent = parent,
user = request.user
)
我看不到任何错误出现的原因。
编辑:这是构造函数的完整代码
def __init __(self,collection_type,user = None,parent =没有,* args,** kwargs):
self.collection_type = collection_type
if self.collection_type =='library':
self.user = user
elif self.collection_type =='书架'或自我收藏_type =='series':
self.parent = parent
else:
raise AssertionError,'collection_type必须是library,bookshelf或series'
super (self .__ class__,self).__ init __(* args,** kwargs)
编辑:Stacktrace
环境:
请求方法:POST
请求URL:http:// localhost: 8000 / forms / create_bookshelf / hello
Django版本:1.1.1
Python版本:2.6.1
安装的应用程序:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'libraries',
'users',
'books',
'django.contrib.admin',
'googlehooks',
'registration']
安装的中间件:
('django。 middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.Authenticat ionMiddleware')
追溯:
文件/Library/Python/2.6/site-packages/django/core/handlers/base.pyin get_response
92. response = callback(request,* callback_args,** callback_kwargs)
文件/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py在__call__
78。 return self.view_func(request,* args,** kwargs)
在create_collection中的文件/Users/marcus/Sites/marcuswhybrow.net/autolib/libraries/forms.py
13. form = CreateCollectionForm (request.POST,collection_type = collection_type,user = request.user)
异常类型:type / at / forms / create_bookshelf / hello
异常值:__init __()获取了多个关键字参数值'collection_type'
你正在传递 但是,你想做的事情永远都不会有效。在 collection_type
参数作为一个关键字参数,因为你特别说 collection_type = collection_type $你调用表单构造函数c $ c>所以Python将它包含在
kwargs
字典中 - 但是因为你也已经将其声明为该函数定义中的位置参数,它会尝试将其传递两次,从而导致错误。 / p>
* args
字典之前,您不能有 user = None,parent = None
,因为那些已经是kwargs,并且args必须始终在kwargs之前。修复它的方法是删除collection_type,user和parent的明确定义,并从函数中的kwargs中提取它们:
collection_type = kwargs.pop('collection_type',None)
user = kwargs.pop('user',None)
parent = kwargs.pop('parent',None)
I get the following error when instantiating a Django form with a the constructor overriden:
__init__() got multiple values for keyword argument 'collection_type'
The __init__()
function (shown below) is exactly as written this but with # code
replaced with my logic. Asside from that I am essentially overriding the form's (which is a ModelForm) constructor.
def __init__(self, collection_type, user=None, parent=None, *args, **kwargs):
# code
super(self.__class__, self).__init__(*args, **kwargs)
The call that creates the error is shown here:
form = CreateCollectionForm(
request.POST,
collection_type=collection_type,
parent=parent,
user=request.user
)
I cannot see any reason why the error is popping up.
EDIT: Here is the full code for the constructor
def __init__(self, collection_type, user=None, parent=None, *args, **kwargs):
self.collection_type = collection_type
if self.collection_type == 'library':
self.user = user
elif self.collection_type == 'bookshelf' or self.collection_type == 'series':
self.parent = parent
else:
raise AssertionError, 'collection_type must be "library", "bookshelf" or "series"'
super(self.__class__, self).__init__(*args, **kwargs)
EDIT: Stacktrace
Environment:
Request Method: POST
Request URL: http://localhost:8000/forms/create_bookshelf/hello
Django Version: 1.1.1
Python Version: 2.6.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'libraries',
'users',
'books',
'django.contrib.admin',
'googlehooks',
'registration']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.6/site-packages/django/contrib/auth/decorators.py" in __call__
78. return self.view_func(request, *args, **kwargs)
File "/Users/marcus/Sites/marcuswhybrow.net/autolib/libraries/forms.py" in create_collection
13. form = CreateCollectionForm(request.POST, collection_type=collection_type, user=request.user)
Exception Type: TypeError at /forms/create_bookshelf/hello
Exception Value: __init__() got multiple values for keyword argument 'collection_type'
You're passing the collection_type
argument in as a keyword argument, because you specifically say collection_type=collection_type
in your call to the form constructor. So Python includes it within the kwargs
dictionary - but because you have also declared it as a positional argument in that function's definition, it attempts to pass it twice, hence the error.
However, what you're trying to do will never work. You can't have user=None, parent=None
before the *args
dictionary, as those are already kwargs, and args must always come before kwargs. The way to fix it is to drop the explicit definition of collection_type, user and parent, and extract them from kwargs within the function:
def __init__(self, *args, **kwargs):
collection_type = kwargs.pop('collection_type', None)
user = kwargs.pop('user', None)
parent = kwargs.pop('parent', None)
这篇关于Django错误:获取关键字参数的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!