Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

python - 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 the purpose I would be very thankful, as I am new to the language.

My models look like:-

 class Employee(Person):
     nickname = models.CharField(_('nickname'), max_length=25, null=True,
         blank=True)
     blood_type = models.CharField(_('blood group'), max_length=3, null=True,
         blank=True, choices=BLOOD_TYPE_CHOICES)
     marital_status = models.CharField(_('marital status'), max_length=1,
         null=True, blank=True, choices=MARITAL_STATUS_CHOICES)
     nationality = CountryField(_('nationality'), default='IN', null=True,
         blank=True)
     about = models.TextField(_('about'), blank=True, null=True)
     dependent = models.ManyToManyField(Dependent,
         through='DependentRelationship')
     pan_card_number = models.CharField(_('PAN card number'), max_length=50,
         blank=True, null=True)
     policy_number = models.CharField(_('policy number'), max_length=50,
         null=True, blank=True)
     # code specific details
     user = models.OneToOneField(User, blank=True, null=True,
         verbose_name=_('user'))

 class Person(models.Model):
      """Person model""" 
      title = models.CharField(_('title'), max_length=20, null=True, blank=True)
      first_name = models.CharField(_('first name'), max_length=100)
      middle_name = models.CharField(_('middle name'), max_length=100, null=True,
          blank=True)
      last_name = models.CharField(_('last name'), max_length=100, null=True,
          blank=True)
      suffix = models.CharField(_('suffix'), max_length=20, null=True,
          blank=True)
      slug = models.SlugField(_('slug'), max_length=50, unique=True)


class PhoneNumber(models.Model) :
     phone_number = generic.GenericRelation('PhoneNumber')
     email_address = generic.GenericRelation('EmailAddress')
     address = generic.GenericRelation('Address')

     date_of_birth = models.DateField(_('date of birth'), null=True, blank=True)
     gender = models.CharField(_('gender'), max_length=1, null=True,
         blank=True, choices=GENDER_CHOICES)

     content_type = models.ForeignKey(ContentType,

If anyone could suggest me a link or so. it would be a great help........

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...