Created
October 6, 2013 15:00
-
-
Save yprez/6855083 to your computer and use it in GitHub Desktop.
Email list field for Django Rest Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.exceptions import ValidationError | |
from django.utils.translation import ugettext_lazy as _ | |
from rest_framework import serializers | |
class EmailListField(serializers.EmailField): | |
default_error_messages = { | |
'invalid': _('Invalid E-mail address.'), | |
} | |
def from_native(self, data): | |
return data | |
def validate(self, value): | |
if not isinstance(value, list): | |
raise ValidationError(_('List of e-mails required!')) | |
if not all(value): | |
# Don't allow empty values | |
raise ValidationError(self.default_error_messages['invalid']) | |
def run_validators(self, value): | |
return [super(EmailListField, self).run_validators(v) for v in value] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment