Reputation: 1192
Trying to figure out most elegant way to accurately get POST data from a form in Django where some input fields are dynamically created by the user.
I haven't quite coded everything so I'll try to describe with sufficient detail:
The form follows the below models.
class Address(models.Model):
title = models.CharField(max_length=200)
instructions = models.TextField()
date_time_added = models.DateTimeField(auto_now=True, null=True, blank=True)
class Contact(models.Model):
address = models.ManyToManyField(Stop, through='Address_Contact')
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
phone_num = models.CharField(max_length=20, default='')
email = models.CharField(max_length=50, default='')
company = models.CharField(max_length=30, default='')
date_time_added = models.DateTimeField(auto_now=True, null=True, blank=True)
class Address_Contact(models.Model):
address = models.ForeignKey(Address, on_delete=models.CASCADE)
contact = models.ForeignKey(Contact, on_delete=models.CASCADE)
date_time_added = models.DateTimeField(auto_now=True, null=True, blank=True)
In the user interface for adding an Address and its associated Contacts, a user can click a button to create more Contact entries on the same page (accomplished via jQuery). So the initial presentation of the form includes the relevant fields for Contact, but by clicking the button 1 or more times, user can add multiple contacts to the same Address.
I know how to use getlist() on the request.POST to get this data.
Now my problem:
For each Contact, it's possible to have multiple phone numbers. Again this is achieved on the front end via jQuery. But how can I get multiple phone numbers from each Contact when there is an arbitrary number of Contacts and Contact Numbers for each? Is there a way to access contents of a list within a list in some kind of ordered way?
For instance, the following is a possible Contact entry for an Address:
contact name: John Johnson
phone number: 828 282 2999
phone number: 399 399 3999
contact name: Steve Murphy
phone number: 399 399 3333
contact name: Mary West
phone number: 399 399 3000
phone number: 299 392 2003
Upvotes: 1
Views: 76
Reputation: 2678
If the requirement is to maintain multiple values.
You should try for Foreign key relationship.
Take a look at this.
Maintain many-to-one relationship
Upvotes: 1