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........
First, create a model that contains fields name and other metadata. It can be used to create a table in database and dynamic HTML form. This file contains a class that inherits ModelForm and mention the model name for which HTML form is created. Write a view function to load the ModelForm from forms.py.
Model Forms are forms that are connected directly to models, allowing them to populate the form with data. It allows you to create a form from a pre-existing model. You add an inline class called Meta, which provides information connecting the model to the form. An inline class is a class within another class.
Model Meta is basically the inner class of your model class. Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name, and a lot of other options. It's completely optional to add a Meta class to your model.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With