Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use forms as fields in other forms?

I have a form that I've created in Django:

class someForm(forms.Form):...

that takes in a variable, someVariable, in its init function:

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

Is it possible for me to use someForm as a field in another form like so?:

class someOtherForm(forms.Form):
    sf = someForm(someVariable=self.someVariable)
...
    def __init__(self, someVariable, *args, **kwargs)
    self.someVariable = someVariable
like image 241
fox Avatar asked Dec 28 '25 14:12

fox


1 Answers

I think your best bet would be extending the original form like so:

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)
like image 158
Lucas Welper Avatar answered Dec 31 '25 17:12

Lucas Welper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!