Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为对象指定一个参数_Python_Django - Fatal编程技术网

Python 为对象指定一个参数

Python 为对象指定一个参数,python,django,Python,Django,如何为ModelForm对象指定一个额外的参数,如下所示: 初始化ModelForm对象 form = ChangeProfile(request.POST, initial={'first_name':costumer.first_name, 'last_name':costumer.last_name}, extraParameter) 我如何在这个类中获取extraParameter: class ChangeProfile(ModelForm): 创造这样一个建设者不是上帝的主意

如何为ModelForm对象指定一个额外的参数,如下所示: 初始化ModelForm对象

form = ChangeProfile(request.POST, initial={'first_name':costumer.first_name, 'last_name':costumer.last_name}, extraParameter) 
我如何在这个类中获取extraParameter:

class ChangeProfile(ModelForm):
创造这样一个建设者不是上帝的主意

def __init__(self, request, initial, extraParamter):

我应该在这里做什么?

对于这种情况,您需要覆盖
\uuuu init\uuu

但是,您的
\uuuu init\uuuu
的签名有一个bug

你应该做:

class ChangeProfile(ModelForm):
    def __init__(self, *args, **kwargs):
        self.extraParameter = kwargs.pop("extraParameter")
        super(ChangeProfile, self).__init__(*args, **kwargs)
        #any other thing you want
从视图:

extraParameter = "hello"

#GET request
form=ChangeProfile(initial={'first_name':costumer.first_name, 'last_name':costumer.last_name}, extraParameter=extraParameter)

#POST request
form=ChangeProfile(request.POST ,initial={'first_name':costumer.first_name, 'last_name':costumer.last_name}, extraParameter=extraParameter)

您可以通过多种方式传递参数,就像一个简单的Python类一样,您必须小心不要破坏Django表单/模型表单的默认行为

class YourForm(forms.Form):
    def __init__(self, custom_arg=None, *args, **kwargs):
        # We have to pop the 'another_arg' from kwargs,
        # because the __init__ from 
        # forms.Form, the parent class, 
        # doesn't expect him in his __init__.
        self.another_arg = kwargs.pop('another_arg', None)

        # Calling the __init__ from the parent, 
        # to keep the default behaviour
        super(YourForm, self).__init__(*args, **kwargs)

        # In that case, just our __init__ expect the custom_arg
        self.custom_arg = custom_arg

        print "Another Arg: %s" % self.another_arg
        print "Custom Arg: %s" % self.custom_arg


# Initialize a form, without any parameter
>>> YourForm()
Another Arg: None
Custom Arg: None
<YourForm object at 0x102cc6dd0>

# Initialize a form, with a expected parameter
>>> YourForm(custom_arg='Custom arg value')
Another Arg: None
Custom Arg: Custom arg value
<YourForm object at 0x10292fe50>

# Initialize a form, with a "unexpected" parameter
>>> YourForm(another_arg='Another arg value')
Another Arg: Another arg value
Custom Arg: None
<YourForm object at 0x102945d90>

# Initialize a form, with both parameters
>>> YourForm(another_arg='Another arg value',
             custom_arg='Custom arg value')
Another Arg: Another arg value
Custom Arg: Custom arg value
<YourForm object at 0x102b18c90>
class YourForm(forms.Form):
定义初始值(self,custom_arg=None,*args,**kwargs):
#我们必须从kwargs那里找到“另一个”,
#因为从
#forms.Form,父类,
#不指望他在他的第一次。
self.other_arg=kwargs.pop('other_arg',无)
#从父级调用_uinit _uu,
#保持默认行为
超级(你的形式,自我)。\uuuuu初始值(*args,**kwargs)
#在这种情况下,只需我们的_uinit _;预期自定义参数
self.custom\u arg=custom\u arg
打印“另一个参数:%s”%self。另一个参数
打印“自定义参数:%s”%self.Custom\u参数
#初始化窗体,不带任何参数
>>>YourForm()
另一个Arg:没有
自定义参数:无
#使用预期参数初始化窗体
>>>您的表单(custom_arg='custom arg value')
另一个Arg:没有
自定义参数:自定义参数值
#使用“意外”参数初始化窗体
>>>YourForm(另一个参数='另一个参数值')
另一个参数:另一个参数值
自定义参数:无
#使用两个参数初始化窗体
>>>YourForm(另一个参数='另一个参数值',
自定义参数=“自定义参数值”)
另一个参数:另一个参数值
自定义参数:自定义参数值