I need to use multiple formsets using custom forms for a single view. I understand that you need to prefix the formsets so that they don't conflict with each other. However, when I try to prefix the formsets, a ValidationError gets raised.
The way I'm creating the formsets is by passing a list of forms to the view, then making a list of FormSets with formset_factory. Then I create a list of the initialized formset开发者_开发问答s.
Here is my code:
def edit_stuff2(request, business_id, template_name="business/edit_info.html",
*args, **kwargs):
business = BusinessDoc.get(business_id)
FormSets=[]
formsets=[]
info_forms = [(EmailDocForm,'emails'), (URLDocForm,'urls')]
for form in info_forms:
FormSets.append(formset_factory(form[0], max_num =0,
can_delete=True, extra=1))
if request.user:
if request.method == "POST":
for FormSet,tup in zip(FormSets,info_forms):
FormSet.form = staticmethod(curry(tup[0], business))
formsets.append(FormSet(request.POST,prefix=tup[1],
initial = business[tup[1]]))
if formsets[-1].is_valid():
for form in formsets[-1].forms:
form.save()
#request.user.message_set.create(message=u"Check it")
formsets = []
for FormSet, tup in zip(FormSets, info_forms):
FormSet.form = staticmethod(curry(tup[0], business))
formsets.append(FormSet(prefix=tup[1], initial=business[tup[1]]))
return direct_to_template(request, template=template_name,
extra_context={'business': business,
'formsets': formsets,
'info_type': info_type})
else:
return HttpResponseRedirect(reverse('home'))
精彩评论