关键字参数有多个值

关键字参数有多个值

本文介绍了Django 错误:关键字参数有多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构造函数被覆盖的情况下实例化 Django 表单时出现以下错误:

I get the following error when instantiating a Django form with a the constructor overriden:

__init__() got multiple values for keyword argument 'collection_type'

__init__() 函数(如下所示)与本文所写的完全一样,只是将 # code 替换为我的逻辑.除此之外,我基本上覆盖了表单(它是一个 ModelForm)的构造函数.

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.

这是构造函数的完整代码

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)

堆栈跟踪

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'

推荐答案

您将 collection_type 参数作为关键字参数传入,因为您特意说了 collection_type=collection_type 在您对表单构造函数的调用中.因此,Python 将它包含在 kwargs 字典中 - 但因为您还在该函数的定义中将其声明为位置参数,所以它尝试传递它两次,因此出现错误.

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.

但是,您尝试做的事情永远不会奏效.你不能有 user=None, parent=None before *args 字典,因为那些已经是 kwargs,并且 args 必须总是出现在夸格之前.修复它的方法是删除 collection_type、user 和 parent 的显式定义,并从函数内的 kwargs 中提取它们:

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 错误:关键字参数有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:35