D
D
Dauren S2017-02-15 12:45:07
Django
Dauren S, 2017-02-15 12:45:07

Django registration checking uniqueness of email in form?

How to check for unique email in the form when registering a user? How to write such a validator?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-02-15
@deliro

def clean_email(self):
    email = self.cleaned_data['email'].strip()
    if User.objects.filter(email__iexact=email).exists():
        raise ValidationError('...')
    return email

In general, at the database level, you also need to make a case insensitive unique index like this (postgres):
CREATE UNIQUE INDEX accounts_email_uniq ON accounts_user USING BTREE ((lower((email)::text)));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question