我有一个在Django中创建的表单:

class someForm(forms.Form):...


在其init函数中包含一个变量someVariable

def __init__(self, someVariable, *args, **kwargs):


我是否可以将someForm用作其他形式的字段?

class someOtherForm(forms.Form):
    sf = someForm(someVariable=self.someVariable)
...
    def __init__(self, someVariable, *args, **kwargs)
    self.someVariable = someVariable

最佳答案

我认为您最好的选择是像这样扩展原始格式:

def someForm(forms.Form):
    someVariable = ...
    ...
    def __init__(self, someVariable, *args, **kwargs):
        self.someVariable = someVariable

def someOtherForm(someForm):
    ...
    def __init__(self, someVariable, *args, **kwargs):
        super(SomeOtherForm, self).__init__(someVariable, *args, **kwargs)

09-25 19:35