Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making email field unique with Django User admin

There was a nearly similar question: How to make email field unique in model User from contrib.auth in Django

The solution was not perfect: Validating email for uniqueness. The solution provided is rather funny. It disallows modifications to User that leave email intact. How to fix it? Thanks in advance!

like image 283
Viet Avatar asked Feb 17 '10 06:02

Viet


2 Answers

in your __init__.py

from django.contrib.auth.models import User

User._meta.get_field_by_name('email')[0]._unique = True
like image 156
Willian Avatar answered Nov 08 '22 08:11

Willian


Thanks to Ofri Raviv but what I've seen is not what I needed. So I resolved my own issue and now I'd like to share the tips:

  1. Use username instead of email, exclude email from the form. Mask its label as email.

  2. Subclass User and create a UNIQUE field which receives email addresses, mask it as email, exclude original email field from the form.

It's that simple but took me some time. Hope it help others with the same need.

like image 23
Viet Avatar answered Nov 08 '22 10:11

Viet