These days I am trying my hand at a rewrite of Dead Man’s Switch, to add more features and generally improve the service.

One of these features was being able to add multiple emails in the “recipients” field, and I wanted to do this as cleanly as possible, so I decided to create a Django model with custom validation. This was not immediately obvious, as the docs didn’t mention much. Eventually I stumbled across this very thing done in the source, and here is my model class, in the hopes that some of you might find it useful:

class MultipleEmailField(models.CharField):
    description = ugettext_lazy("E-mail address")

    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.RegexField,
            'regex': emails_re,
            'max_length': self.max_length,
            'error_messages': {
                'invalid': _(u'Enter valid emails, separated by commas.'),
        return super(MultipleEmailField, self).formfield(**defaults)