Question

I define three ModelForms, say A,B,C, they have different fields.

I want the user to be able to create a certain serial of Models. Say I am going to arrange the forms 'AABBCC' in a page. How can I group them into a single form so the user can fill them within one time?

I think formset won't finish the job but I don't know if there are some advanced features I don't know which enable it.

Was it helpful?

Solution

Perhaps you're just thinking that it's difficult while it actually isn't? :)

def edit(request):
    AFormSet = formset_factory(AForm)
    BFormSet = formset_factory(BForm)
    CFormSet = formset_factory(CForm)

    data = request.POST or None
    formsets = [
        AFormSet(data, prefix='A'),
        BFormSet(data, prefix='B'),
        CFormSet(data, prefix='C'),
    ]

    if all(f.is_valid() for f in formsets):
        for f in formsets:
            f.save()

    return render_to_response('edit.html', {'formsets': formsets})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top