python - 使用模型表单中的 Generic_inlineformset_factory 创建表单

标签 python django

我想借助 ModelForm 创建一个编辑表单。

我的模型包含一个通用关系 b/w 类,所以如果有人可以向我建议 View 和一些模板,我将非常感激,因为我是该语言的新手。

我的模型看起来像:-

 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,

如果有人可以建议我一个链接左右。这将是一个很大的帮助.........

最佳答案

发布我找到的解决方案。查看 Generic_inlineformset_factory 的源代码后。

我的观点是:-

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))

这很成功,我认为如果有人在他们的模型中使用通用关系,并且想创建一个表单来编辑该信息,这将是一个很好的帮助。

关于python - 使用模型表单中的 Generic_inlineformset_factory 创建表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4304148/

相关文章:

python - 如果没有给出参数, 'yield' 会返回什么?

python - 如何使用 OpenCV2 在带有黑色边框的图像上显示白色文本?

python - Django project/admin 站点匹配查询不存在

python - Django admin - 'NoneType' 对象没有属性 'rindex'

django - 为什么我需要一个单独的 Django 网络服务器?

python - 使用 slug 字段找不到反向

python - Python 中的网络爬虫

python - 如何在 Tensorflow RNN 中构建嵌入层?

python - 在 "for line in data"循环中使用 .readline() 将文件的多行分配给多个变量

python - 没有更改的PATCH请求应返回什么状态码?