Django: multiple models in one template using forms [closed] Django: multiple models in one template using forms [closed] python python

Django: multiple models in one template using forms [closed]


This really isn't too hard to implement with ModelForms. So lets say you have Forms A, B, and C. You print out each of the forms and the page and now you need to handle the POST.

if request.POST():    a_valid = formA.is_valid()    b_valid = formB.is_valid()    c_valid = formC.is_valid()    # we do this since 'and' short circuits and we want to check to whole page for form errors    if a_valid and b_valid and c_valid:        a = formA.save()        b = formB.save(commit=False)        c = formC.save(commit=False)        b.foreignkeytoA = a        b.save()        c.foreignkeytoB = b        c.save()

Here are the docs for custom validation.


I just was in about the same situation a day ago, and here are my 2 cents:

1) I found arguably the shortest and most concise demonstration of multiple model entry in single form here: http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/ .

In a nutshell: Make a form for each model, submit them both to template in a single <form>, using prefix keyarg and have the view handle validation. If there is dependency, just make sure you save the "parent" model before dependant, and use parent's ID for foreign key before commiting save of "child" model. The link has the demo.

2) Maybe formsets can be beaten into doing this, but as far as I delved in, formsets are primarily for entering multiples of the same model, which may be optionally tied to another model/models by foreign keys. However, there seem to be no default option for entering more than one model's data and that's not what formset seems to be meant for.


I very recently had the some problem and just figured out how to do this.Assuming you have three classes, Primary, B, C and that B,C have a foreign key to primary

    class PrimaryForm(ModelForm):        class Meta:            model = Primary    class BForm(ModelForm):        class Meta:            model = B            exclude = ('primary',)    class CForm(ModelForm):         class Meta:            model = C            exclude = ('primary',)    def generateView(request):        if request.method == 'POST': # If the form has been submitted...            primary_form = PrimaryForm(request.POST, prefix = "primary")            b_form = BForm(request.POST, prefix = "b")            c_form = CForm(request.POST, prefix = "c")            if primary_form.is_valid() and b_form.is_valid() and c_form.is_valid(): # All validation rules pass                    print "all validation passed"                    primary = primary_form.save()                    b_form.cleaned_data["primary"] = primary                    b = b_form.save()                    c_form.cleaned_data["primary"] = primary                    c = c_form.save()                    return HttpResponseRedirect("/viewer/%s/" % (primary.name))            else:                    print "failed"        else:            primary_form = PrimaryForm(prefix = "primary")            b_form = BForm(prefix = "b")            c_form = Form(prefix = "c")     return render_to_response('multi_model.html', {     'primary_form': primary_form,     'b_form': b_form,     'c_form': c_form,      })

This method should allow you to do whatever validation you require, as well as generating all three objects on the same page. I have also used javascript and hidden fields to allow the generation of multiple B,C objects on the same page.