Skip to content

Instantly share code, notes, and snippets.

@yprez
Created October 6, 2013 15:00
Show Gist options
  • Save yprez/6855083 to your computer and use it in GitHub Desktop.
Save yprez/6855083 to your computer and use it in GitHub Desktop.
Email list field for Django Rest Framework
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