'Django saving multiple forms - what's a compact way of doing this?
Is there a more compact way of writing this in Python / Django?
I haven' finished this yet, but I can see it growing big quickly, as I need to include further branches for 3 other values of form7.cleaned_data.get('working_type_selection') == '3 other Options', apart from 'Employee'. If it's Employee form8 gets saved, otherwise it could be form 9, or form 10.
The saving of the forms has to be the final step of each branch right?
if form1.is_valid() and form2.is_valid():
if form2.cleaned_data.get('right_to_work_selection') == 'Passport' and form3.is_valid():
if form7.cleaned_data.get('working_type_selection') == 'Employee' and form8.is_valid():
form1.save()
form2.save()
form3.save()
form7.save()
form8.save()
return redirect('profile_employment')
if form2.cleaned_data.get('right_to_work_selection') == 'Birth Certificate & N.I. Number' and form4.is_valid() and form6.is_valid():
if form7.cleaned_data.get('working_type_selection') == 'Employee' and form8.is_valid():
form1.save()
form2.save()
form4.save()
form6.save()
form7.save()
form8.save()
return redirect('profile_employment')
if form2.cleaned_data.get('right_to_work_selection') == 'Certificate of Registration & N.I. Number' and form5.is_valid() and form6.is_valid():
if form7.cleaned_data.get('working_type_selection') == 'Employee' and form8.is_valid():
form1.save()
form2.save()
form5.save()
form6.save()
form7.save()
form8.save()
return redirect('profile_employment')
UPDATE
I've tried to create a dictionary, but I am not sure how it would work if I need to branch out depending on the value of form 7??
forms = {
"Passport": {
"RightToWork Validation": (form3),
"Save": (form1, form2, form3, form7, form8),
},
"Birth Certificate & N.I. Number": {
"RightToWork Validation": (form4, form6),
"Save": (form1, form2, form4, form6, form7, form8),
},
"Certificate of Registration & N.I. Number": {
"RightToWork Validation": (form5, form6),
"Save": (form1, form2, form5, form6, form7, form8),
},
}
if form1.is_valid() and form2.is_valid():
right_to_work = form2.cleaned_data.get('right_to_work_selection')
if all([f.is_valid()] for f in forms[right_to_work]["RightToWork Validation"]):
if form7.cleaned_data.get('working_type_selection') == 'Employee' and form8.is_valid():
for form in forms[right_to_work]["Save"]:
form.save()
return redirect('profile_employment')
if form7.cleaned_data.get('working_type_selection') == 'Limited Co' and form9.is_valid():
# only additional forms, form 7 and 9 should be saved
if form7.cleaned_data.get('working_type_selection') == 'Sole Trader' and form10.is_valid():
# only additional forms, form 7 and 10 should be saved
Solution 1:[1]
You can organize the forms you need to validate and save into a dictionary.
forms = {
"Passport": {
"Validation": (form3,),
"Save": (form1, form2, form3, form7, form8),
},
"Birth Certificate & N.I. Number": {
"Validation": (form4, form6),
"Save": (form1, form2, form4, form6, form7, form8),
},
"Certificate of Registration & N.I. Number": {
"Validation": (form6,),
"Save": (form1, form2, form5, form6, form7, form8),
},
}
if form1.is_valid() and form2.is_valid():
selection = form2.cleaned_data.get('right_to_work_selection')
if all([f.is_valid() for f in forms[selection]["Validation"]) and \
form7.cleaned_data.get('working_type_selection') == 'Employee' and \
form8.is_valid():
for form in forms[selection]["Save"]:
form.save()
return redirect('profile_employment')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
