both forms in formset need to be selected
I have a formset that have two forms.
forms:
class PresClinicForm(forms.Form):
_names = list(PresClinic.objects.values_list('pres_clinic_id', 'pres_clinic_name'))
_names.append(["New", u'Nova entrada'])
pres_name = forms.ChoiceField(widget=RadioSelect(), choices=_na开发者_如何学Gomes, label=
"", required=True)
PresClinicFormSet = formset_factory(PresClinicForm, extra=2)
views:
if request.method == 'POST':
formset1 = PresClinicFormSet(request.POST, request.FILES, prefix='pres_clinic')
if formset1.is_valid():
choice = formset1.cleaned_data
return render_to_response('template.html', {'options': options})
template:
<form method="post" action="">
<div>
{{ formset1.management_form}}
{% for form in formset1.forms %}
{{ form }}
{% endfor %}
<input type="submit" value="Guardar" />
</div>
</form>
The user has to select one option in each form.
I tried required=True into forms.py but if I select only one option, it works anyway..It shouldn't work if the user only select an option. That's what I trying to implement..
Does someone knows how to do this?
Thanks in advance for any help!
You can use Matthew Flanagan's code for requiring at least one form in a formset to be valid as a starting point and integrate that into your workflow: http://wadofstuff.blogspot.com/2009/08/requiring-at-least-one-inline-formset.html
Hope that helps you out.
Please check Mark's question for this answer: Django: Make all formset forms required?
Be careful, you can only use formset with django 1.3, with django 1.1 cleaned_data gives an error.
精彩评论