我想在ModelForm的帮助下创建一个编辑表单。
并且我的模型包含一个通用关系b / w类,所以如果任何人可以建议我的视图和一些模板用于此目的我将非常感谢,因为我是该语言的新手。
我的模特看起来像: -
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,
如果有人可以建议我链接左右。这将是一个很大的帮助........
答案 0 :(得分:16)
发布我发现的解决方案。看一下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))
这项工作成功,我认为如果任何人在他们的模型中使用Generic Relation,并希望创建一个用于编辑该信息的表单,这将是一个很好的帮助。