Django:关键字参数"initial"的多个值 [英] Django: multiple values for keyword argument 'initial'
问题描述
在Django上,我得到了此追溯:
On Django I'm getting this traceback:
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/db/transaction.py" in inner
223. return func(*args, **kwargs)
File "/Users/eeytan/ddragon/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
1085. form = ModelForm(request.POST, request.FILES, instance=obj)
Exception Type: TypeError at /admin/dragon_portal/parentprofile/1/
Exception Value: __init__() got multiple values for keyword argument 'instance'
对于基于此代码的代码,请创建一个Django表单以保存两个模型:
class ParentCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
email = forms.EmailField()
#ice_contact = forms.CharField(max_length=100)
#notes = HTMLField()
def __init__(self, instance=None, *args, **kwargs):
_fields = ('username', 'first_name', 'last_name', 'email', 'password')
_initial = model_to_dict(instance.dragonuser, _fields) \
if instance is not None else {}
kwargs['initial'] = _initial
super(ParentCreationForm, self).__init__(instance=instance, *args, **kwargs)
self.fields.update(fields_for_model(DragonUser, _fields))
如您所见, __ init __()
的签名未更改.而且,更奇怪的是,Django更详细的回溯显示,对于回溯中的每个步骤, argv
总是 , {}
,所以我我什至不知道错误来自哪一点追溯.
As you can see, the signature of __init__()
is not changed. Also, more strangely, Django's more detailed traceback shows that for each step in the traceback, the argv
is always, {}
, so I'm not even sure at what point of the traceback the error comes from.
推荐答案
对于ModelForm, __ init __
函数签名不正确.
The __init__
function signature is incorrect for a ModelForm.
来自 BaseModelForm的Django来源您可以看到函数签名是:
From the Django source for BaseModelForm you can see that the function signature is:
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=None,
empty_permitted=False, instance=None):
因此,如果有人像Django那样在第一个位置使用未命名的参数实例化您的表单,那么您将最终遇到此错误.
So if anyone instantiates your form with an unnamed argument in the first position as Django does, then you'll end up with this error.
我建议像这样重写您的 __ init __
:
I'd suggest rewriting your __init__
like this:
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance')
_fields = ('username', 'first_name', 'last_name', 'email', 'password')
_initial = model_to_dict(instance.dragonuser, _fields) \
if instance is not None else {}
kwargs['initial'] = _initial
super(ParentCreationForm, self).__init__(*args, **kwargs)
self.fields.update(fields_for_model(DragonUser, _fields))
这篇关于Django:关键字参数"initial"的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!