Monday, April 30, 2012

How to create form from odd model

I am working with an existing database where I am unable to change the
schema and having some trouble trying to deal with presenting forms.
The structure in question is as follows and all models are unmanaged.

class Persons(models.Model):
personid = models.BigIntegerField(primary_key=True,

class Phones(models.Model):
phoneid = models.BigIntegerField(primary_key=True,
number = models.CharField(max_length=60, db_column='Number',
type = models.CharField(max_length=15, db_column='Type',

class Personsphones(models.Model):
personphoneid = models.BigIntegerField(primary_key=True,
personid = models.ForeignKey(Persons, db_column='PersonID')
phoneid = models.ForeignKey(Phones, db_column='PhoneID')

I want to create a form to display all of the 'Phones' associated with
a particular 'Persons' and in addition be able to modify/add/remove
'Phones' belonging to a 'Persons'. Right now the only thing I can
think of is to display the 'Phones' in a modelformset and then if one
is added or removed manually set the 'Personsphones' relation. Any
ideas on how to best deal with this model setup? If I manually need to
update the relationships should I just create a custom save() for my
'Phones' form?

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment