Django default auth user but unique email

464 views
Skip to first unread message

Bruce Li

unread,
Jan 26, 2015, 9:56:14 PM1/26/15
to django...@googlegroups.com
Hi,

I want to build my user management based on default django users. But I want emails to be unique as well. Is it possible to do that without the need to create a custom user model and user managment?


Thanks,
- Bruce

Bruce Li

unread,
Jan 26, 2015, 10:01:46 PM1/26/15
to django...@googlegroups.com
To be more specific, I want the username and email in default user model both to be unique and not blank.

James Schneider

unread,
Jan 27, 2015, 1:36:53 AM1/27/15
to django...@googlegroups.com

If you want any sort of enforcement of a unique email address at the database level, you'll need a custom user model. If you don't care about that, all you'd need to do is override the user forms and create a custom clean_email function that checks for duplicates before saving the form data. Not 100% guaranteed on a high traffic site unless you wrap it in a transaction, and will become expensive to check as you gather more users in the database, especially since the email field won't be indexed using the standard user model, and would result in a slower string-matching query against the entire table rather than an index-based comparison.

If you have any special requirements for the user in your application, I highly recommend you bite the bullet and create a custom user model. It's not terribly difficult, and will better posture you down the road in the event that your requirements change, not to mention you can enforce uniqueness at the database level. 

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e415b13a-2b38-4f1b-80e6-4ba808a09175%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruce Li

unread,
Feb 6, 2015, 10:18:52 PM2/6/15
to django...@googlegroups.com
Thanks for your advice, I was too busy to reply. I will learn to use a custom model. Thanks.
Reply all
Reply to author
Forward
0 new messages