问题描述
您好,我正在尝试使用修改的 __ init __
表单方法,但是我遇到以下错误:
Hello, I'm trying to use a modified __init__
form method, but I am encountering the following error:
TypeError
__init__() got multiple values for keyword argument 'vUserProfile'
我需要通过 UserProfile
到我的表单,以获得 dbname
字段,我认为这是一个解决方案(我的表单代码):
I need to pass UserProfile
to my form, to get to dbname
field, and I think this is a solution (my form code):
class ClienteForm(ModelForm):
class Meta:
model = Cliente
def __init__(self, vUserProfile, *args, **kwargs):
super(ClienteForm, self).__init__(*args, **kwargs)
self.fields["idcidade"].queryset = Cidade.objects.using(vUserProfile.dbname).all()
调用构造函数 ClienteForm()
是成功的,并向我显示正确的形式。但是,当提交表单并使用POST调用构造函数时,我会得到以前描述的错误。
Calls to constructor ClienteForm()
without POST are successful and show me the correct form. But when the form is submitted and the constructor is called with POST, I get the previously described error.
推荐答案
表单的 __ init __
方法的签名,以便 vUserProfile
是第一个参数。但是在这里:
You've changed the signature of the form's __init__
method so that vUserProfile
is the first argument. But here:
formPessoa = ClienteForm(request.POST, instance=cliente, vUserProfile=profile)
你通过 request.POST
作为第一个参数 - 除了这将是解释为 vUserProfile
。然后你也尝试通过 vUserProfile
作为关键字arg。
you pass request.POST
as the first argument - except that this will be interpreted as vUserProfile
. And then you also try to pass vUserProfile
as a keyword arg.
真的,你应该避免更改方法签名,并从 kwargs
中获取新数据:
Really, you should avoid changing the method signature, and just get the new data from kwargs
:
def __init__(self, *args, **kwargs):
vUserProfile = kwargs.pop('vUserProfile', None)
这篇关于Django表单__init __()获取了关键字参数的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!