Okay, this one is pretty obvious to everyone who use Django and frequently asked by newbies, but I'd like to make it clear and discuss if there are any other ways to do it. The most widespread and convenient approach now is to store email in username field as Django 1.2 allows "@", "_" and "-" characters, but this way has following issues:
- The worst one: username field is restricted by
max_length=30
property, which is ridiculously small for emails. Even if you override form validation, DB will havevarchar(30)
instead ofEmailField
'svarchar(75)
unless you alter your table manually. - You need to store your email data both in username and email field to make
User.email_user()
working. I think there are some other places whenUser.email
is used. - Code readability fail. Sure, other djangonauts know about this pitfall, but treating field called 'username' (especially when there is still email field) as email obviously makes your code less understandable.
The other approach could be authentication using email
field by passing it to your auth backend like so, but it still has problems:
authenticate(self, email=None, password=None)
User.email
doesn't haveunique=True
property, which means that your DB won't have index, making your lookups by email slow like hell.- You have to deal with
username
field, which hasunique=True
, by completely removing it from your table or altering it to allow NULL and removing index.
Resuming, both ways are evil and require DB-specific code to be executed after syncdb, which is unacceptable if you need DB-independent application.