Skip to content Skip to sidebar Skip to footer

Creating Form Using Generic_inlineformset_factory From The Model Form

I wanted to create a edit form with the help of ModelForm. and my models contain a Generic relation b/w classes, so if any one could suggest me the view and a bit of template for t

Solution 1:

posting the solution I found out. After taking a look at the source of Generic_inlineformset_factory.

I made my view as:-

def edit_contact(request):
     c={}
     profile = request.user.get_profile()
     EmployeeFormSet = generic_inlineformset_factory(PhoneNumber,extra=0,can_delete=False)
     EmployeeFormSet1=generic_inlineformset_factory(EmailAddress,extra=0,can_delete=False)
     EmployeeFormSet2 = generic_inlineformset_factory(Address, extra = 0, can_delete=False)
     if request.method == "POST":
        p_formset = EmployeeFormSet(data=request.POST, instance = profile),
        e_formset = EmployeeFormSet1(data=request.POST, instance = profile),
        a_formset = EmployeeFormSet2(data=request.POST, instance = profile),
        for e in p_formset:
           if e.is_valid():
             e.save()
         for e in e_formset:
           if e.is_valid():
              e.save()
         for e in a_formset:
           if e.is_valid():
              e.save()
         return HttpResponseRedirect('/forms/sucess-edit/')
      else:
          p_formset = EmployeeFormSet(instance = profile),
          e_formset = EmployeeFormSet1(instance = profile),
          a_formset = EmployeeFormSet2(instance = profile),
      c.update({'p_formset': p_formset, 'e_formset': e_formset,'a_formset': a_formset})
      return  render_to_response('forms/edit_contact.html',c,
                                    context_instance=RequestContext(request))

This worked successfully, I think it would be a good help if any one is using the Generic Relation in their model, and want to create a form for editing that information.

Post a Comment for "Creating Form Using Generic_inlineformset_factory From The Model Form"