Reputation: 44968
I have two forms like this:
class PersonForm(forms.Form):
name = forms.CharField()
def clean(self):
print "Validating Person"
def save(self):
print "Saving Person"
def InstrumentForm(forms.Form):
instrument = forms.CharField()
def clean(self):
print "Validating Instrument"
def save(self):
print "Saving Instrument"
I need to declare another form which subclasses both these forms. I'm using this method as an
alternative to FormWizard
.
def BandForm(PersonForm, InstrumentForm):
def clean(self):
print "Validating Band"
def save(self):
print "Saving Band"
Can I do this? When I call the validate on the BandForm
I would like the validation first occur on the PersonForm
and then on the InstrumentForm
. When I call the save
method of the BandForm
, I would like first save the data in the InstrumentForm
and then on the BandForm
.
What I'm basically trying to do is to save data to both forms together. I was having trouble with FormWizard
and thought that it might be possible to do it like this.
Thanks.
Upvotes: 0
Views: 260
Reputation: 6323
Put both forms inside one form tag (see: https://docs.djangoproject.com/en/1.3/ref/forms/api/#prefixes-for-forms), then validate and save them in order you want, for example in view:
form1 = PersonForm(request.POST or None, prefix='person')
form2 = InstrumentForm(request.POST or None, prefix='instrument')
if form1.is_valid() and form2.is_valid():
form2.save()
form1.save()
# return redirect
# render forms
Upvotes: 1