Login

models.MultipleEmailField

Author:
fero
Posted:
January 14, 2012
Language:
Python
Version:
Not specified
Tags:
models fields validators
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. FormHandler - take the legwork out of form processing by zvoase 6 years, 6 months ago
  2. Add validation for 'unique' and 'unique_together' constraints to newforms created dynamically via form_for_model or form_for_instance by bikeshedder 7 years, 10 months ago
  3. Enforce site wide login by chbrown 6 years, 5 months ago
  4. SeparatedValuesField by jezdez 7 years, 3 months ago
  5. Custom FileField with content type and size validation by nemesis 4 years, 6 months ago

Comments

Please login first before commenting.