[英]A Django form for entering a 0 to n email addresses
我有一個Django應用程序,其中包含一些相當常見的模型: UserProfile
和Organization
。 UserProfile
或Organization
可以有0到n個電子郵件,因此我有一個具有GenericForeignKey
的Email
模型。 UserProfile
和Organization
Models都有一個稱為emails
的GenericRelation
,它指向Email
模型(下面提供的摘要代碼)。
問題 :提供Organization
表單的最佳方式是什么,允許用戶輸入組織詳細信息,包括0到n個電子郵件地址?
我的Organization
創建視圖是基於Django類的視圖 。 我傾向於創建一個動態表單並使用Javascript啟用它,以允許用戶根據需要添加盡可能多的電子郵件地址。 我將使用django-crispy-forms和django-floppyforms呈現表單,以便在使用Twitter Bootstrap的網站上顯示。
我已經考慮過使用嵌入在表單中的BaseGenericInlineFormset
來做這BaseGenericInlineFormset
,但這對於電子郵件地址來說似乎BaseGenericInlineFormset
過分。 將表單集嵌入由基於類的視圖提供的表單中也很麻煩。
請注意, Organization
字段phone_numbers
和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'
您可以嘗試使用.split(),這樣您的表單看起來會更容易,用戶不必繼續添加文本字段。 你可以做的是,制作一個文本框,用戶可以在其中添加多個電子郵件並將其分開。 然后在你的觀點中你可以做到這一點
email = emails.split(',')
for i in emails:
#assign email and save.
如果有電子郵件類型
建立這樣的系統可能仍然是一個好主意。 你能做的是
ABC @ gmail.com工作,XYZ @ k.com學校
然后你就可以像這樣分開它們
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.