Let's say I want a system where 5 people want to register a service at once, all starting at the same date.
Explicit: 5 name fields (passed with extra=5) and just one date field.
I have tried with BaseFormSet and add_fields, but then I get five date fields too.
An example forms.py:
class NameForm(forms.Form):
name = forms.CharField()
class DateForm(form.Form):
date = forms.DateField()
An exam开发者_如何转开发ple views.py:
NameFormSet = formset_factory(NameForm, extra=5)
#The line under will not work, but illustrates what I want to do.
NameFormSet.append(DateForm)
if request.method = 'POST':
formset = NameFormSet(request.POST)
#Do validation etc..
else:
formset = NameFormSet()
return render_to_response('template.html', { 'formset' : formset })
Please help =)
Can you just include another DateForm like so?
NameFormSet = formset_factory(NameForm, extra=5)
if request.method = 'POST':
formset = NameFormSet(request.POST)
date_form = DateForm(request.POST)
if formset.is_valid() and date_Form.is_valid():
date = date_form.cleaned_data['date']
for form in formset:
name = form.cleaned_data['name']
# replace registration with registration model name
registration = Registration(name=name, date=date)
registration.save()
return
else:
formset = NameFormSet()
date_form = DateForm()
return render_to_response('template.html', { 'formset' : formset, 'date_form' : date_form })
精彩评论