0

我有一个 Django 应用程序,其中包含一些相当常见的模型:UserProfileOrganization. AUserProfile或 anOrganization都可以有 0 到 n 封电子邮件,所以我有一个Email具有GenericForeignKey. UserProfileOrganization模型都有一个指向模型的GenericRelation调用(下面提供的摘要代码)。emailsEmail

问题Organization:提供允许用户输入组织详细信息(包括 0 到 n 个电子邮件地址)的表单的最佳方式是什么?

我的Organization创建视图是基于Django 类的视图。我倾向于创建一个动态表单并使用 Javascript 启用它,以允许用户根据需要添加尽可能多的电子邮件地址。我将使用 django-crispy-forms 和 django-floppyforms 呈现表单,以便使用 Twitter Bootstrap 在网站上显示。

我曾考虑过BaseGenericInlineFormset在表单中​​嵌入一个来做到这一点,但这对于电子邮件地址来说似乎有点矫枉过正。将表单集嵌入到由基于类的视图提供的表单中也很麻烦。

请注意,Organization字段phone_numbers和也会出现同样的问题locations

代码

电子邮件.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'

组织.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'
4

1 回答 1

2

您可以尝试使用 .split(),这样您的表单看起来会更容易,用户不必继续添加文本字段。您可以做的是,创建一个文本框,用户可以在其中添加多封电子邮件并用逗号分隔它们。然后在你看来你可以这样做

email = emails.split(',')
for i in emails:
    #assign email and save.

如果有电子邮件类型

构建这样的系统可能仍然是一个好主意。你能做的是

abc@gmail.com-work,xyz@k.com-school

然后你可以像这样拆分它们

email-type=email.split(',')
for i in email-type:
    email=i.split('-')[0]
    if i.split('-')[1]:
        type=i.split('-')[1]
    else:
        #give it a default type
于 2012-10-15T19:13:26.537 回答