Login

models.MultipleEmailField

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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.