Problem related to forms using Models
I have model Person
or say Profile and this class Person
has a genericRelationship with the phonenumber class.Now I wanted to generate a ModelForm which displays the option of adding two or three contact numbers at a time in a single form. Is this possible ?
My models look like :
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)
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)
class PhoneNumber(models.Model):
"""Phone Number model."""
PHONE_LOCATION_CHOICES = (
('w', _('Work')), 315 ('m', _('Mobile')),
('f', _('Fax')),
('p', _('Pager')),
('h', _('Home')),
('o', _('Other')),
)
content_type = models.ForeignKey(ContentType, limit_choices_to{'app_label': 'contacts'})
object_id = models.IntegerField(db_index=True)
content_object = generic.GenericForeignKey()
phone_number = models.CharField(_('number'), max_length=50)
location = models.CharField(_('location'), max_length=1,
choices=PHONE_LOCATION_CHOICES, default='w')
date_added = models.DateTimeField(_('date added'), auto_now_add=True)
date_modified = models.DateTimeField(_('date modified'), auto_now=True)
Then I wanted to make a form for editing the contacxt details.
I would be very thankful to yoy! Thank You!
Regards
As per Django Doc
Generic Relations : let an object have a foreign key to any object through a content-type/object-id field. A GenericForeignKey field can point to any object, be it animal, vegetable, or mineral.
This means you can do what you are saying you want to do.
UPDATE: Adding some code. This would be your forms.py
class EditForm(forms.Form):
title = forms.CharField(label='Username', max_length=30)
phone_num1= forms.CharField(label='Phone Number1',widget=forms.TextInput())
phone_num2= forms.CharField(label='Phone Number2',widget=forms.TextInput())
This could be your views.py
. In your HTML code provide an EDIT link, which has the url in some format. This url format should be mapped in your urls.py file. Which would invoke this fn. in views.py
.
if request.GET.has_key('edit'): # already there - EDIT
nameid = request.GET['edit']
try:
person = Person.objects.get(id=nameid)
phonenum = PhoneNumber.objects.get(user=request.user, person=person)
pnums = ' '.join(n.phone_number for n in person.phonenum_set.all())
except (PhoneNumber.DoesNotExist, Person.DoesNotExist):
raise
form = EditForm({'title:person.title, 'phonunumbers': phonenums})
This is the general idea. There might be some syntax errror as I am typing this in the browser...
精彩评论