- Author:
- fero
- Posted:
- January 14, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
Define validator multiple_email_validator
that splits value by commas and calls validate_email
validator for each element found.
Then define MultipleEmailField with this default validator and augmented max_length.
Then ... use it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.db import models
def multiple_email_validator(value):
emails = value.split(',')
for email in emails:
try:
validate_email(email.strip())
except ValidationError as e:
raise ValidationError(_(u'Enter a valid e-mail address separated by commas.'), code='invalid')
class MultipleEmailField(models.CharField):
default_validators = [validators.multiple_email_validator]
description = _("Multiple e-mail address")
def __init__(self, *args, **kwargs):
kwargs['max_length'] = kwargs.get('max_length', 512)
super(MultipleEmailField, self).__ini__(*args, **kwargs)
def formfield(self, **kwargs):
raise NotImplementedError("here you can return a custom forms.MultipleEmailField")
class MyModel(models.Model):
mymultipleemail = MultipleEmailField()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.