I have a Model that looks something like this:
class Business(models.Model):
name = models.CharField('business name', max_length=100)
# ... some other fields
emails = models.ManyToManyField(Email, null=True)
phone_numbers = models.ManyToManyField(PhoneNumber, null=True)
urls = models.ManyToManyField(URL, null=True)
and a corresponding ModelForm:
class BusinessContactForm(forms.ModelForm):
emails = forms.CharField(widget=forms.Textarea(attrs={'rows':4,'cols':32}))
phone_numbers = forms.CharField(widget=forms.Textarea(attrs={'rows':4,'cols':32}))
urls = forms.CharField(widget=forms.Textarea(attrs={'rows':4,'cols':32}))
class Meta:
model = Business
fields = ['emails', 'phone_numbers', 'urls',]
My question: What is the best way to load the existing emails, phone_numbers, and urls into the Textarea widgets when presenting the form (one per line in their respective widgets)?
Then, after the form has been modified and submitted, what is the best way to make sure to add any new emails, numbers, or urls (m2m relationships) and remove any that are no longer in the list (also making sure not to add duplicates)?