A Django form for entering a 0 to n email addresses

Posted by Erik on Stack Overflow See other posts from Stack Overflow or by Erik
Published on 2012-10-14T21:33:31Z Indexed on 2012/10/14 21:36 UTC
Read the original article Hit count: 422

I have a Django application with some fairly common models in it: UserProfile and Organization. A UserProfile or an Organization can both have 0 to n emails, so I have an Email model that has a GenericForeignKey. UserProfile and Organization Models both have a GenericRelation called emails that points back to the Email model (summary code provided below).

The question: what is the best way to provide an Organization form that allows a user to enter organization details including 0 to n email addresses? My Organization create view is a Django class-based view. I'm leading towards creating a dynamic form and an enabling it with Javascript to allow the user to add as many email addresses as necessary. I will render the form with django-crispy-forms.

I've thought about doing this with a formset embedded within the form, but this seems like overkill for email addresses. Embedding a formset in a form delivered by a class-based view is cumbersome too.

Note that the same issue occurs with the Organization fields phone_numbers and locations.

emails.py:

from django.db import models
from parent_mixins import Parent_Mixin

class Email(Parent_Mixin,models.Model):
    email_type = models.CharField(blank=True,max_length=100,null=True,default=None,verbose_name='Email Type')
    email = models.EmailField()

    class Meta:
        app_label = 'core'

organizations.py:

from emails import Email
from locations import Location
from phone_numbers import Phone_Number
from django.contrib.contenttypes import generic
from django.db import models

class Organization(models.Model):
    active = models.BooleanField()
    duns_number = models.CharField(blank=True,default=None,null=True,max_length=9)  # need to validate this
    emails = generic.GenericRelation(Email,content_type_field='parent_type',object_id_field='parent_id')
    legal_name = models.CharField(blank=True,default=None,null=True,max_length=200)
    locations = generic.GenericRelation(Location,content_type_field='parent_type',object_id_field='parent_id')
    name = models.CharField(blank=True,default=None,null=True,max_length=200)
    organization_group = models.CharField(blank=True,default=None,null=True,max_length=200)
    organization_type = models.CharField(blank=True,default=None,null=True,max_length=200)
    phone_numbers = generic.GenericRelation(Phone_Number,content_type_field='parent_type',object_id_field='parent_id')
    taxpayer_id_number = models.CharField(blank=True,default=None,null=True,max_length=9)  # need to validate this

    class Meta:
        app_label = 'core'

parent_mixins.py

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.db import models

class Parent_Mixin(models.Model):
    parent_type = models.ForeignKey(ContentType,blank=True,null=True)
    parent_id = models.PositiveIntegerField(blank=True,null=True)
    parent = generic.GenericForeignKey('parent_type', 'parent_id')

    class Meta:
        abstract = True
        app_label = 'core'

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms