3

I'm getting this error message on my email field but i'm using the built in django auth system. Is there an easy way to override it. When the user registers the email address is added into the built in field within the built in user system.

Would be great if it's possible to make it extend it over 30 chars due to the nature of the site.

JDavies
  • 2,730
  • 7
  • 34
  • 54

2 Answers2

3

That is one of the issues with using email addresses for user names in Django. Many, many emails are over 30-characters. One common way to address this issues is using a custom "Authentication Backend" for email authentication. Using your own backend you can authenticate a user based on the email field instead of the username field. You can then generate the username based on that email address or using random generated usernames.

You can read more about this approach on my blog post Django Authentication using an Email Address.

Micah Carrick
  • 9,967
  • 3
  • 31
  • 43
  • Thanks for your reply Micah, I will take a little read. The only thing is the site i'm working on fairly big and has a few hundred users already. Would this matter or is it really something that would need to be looked at from the initial site build? p.s. Thanks for the link the to blog, will be visiting a lot more often :) – JDavies Oct 12 '12 at 08:29
  • Should be fine. In fact, in my sites I usually have both backends--the user can use a user name *or* an email address and that can easily be implemented into an existing system.Just check to make sure you don't have existing duplicate email addresses in `auth_user` first. – Micah Carrick Oct 12 '12 at 13:22
  • Thanks, i've had a word with our other dev and i think were going to go ahead with your solution. Thanks again for your help! – JDavies Oct 23 '12 at 11:28
  • Your link is down, please put the full answer here or update your answer to provide another location please. – Fadi Nov 13 '17 at 18:49
2

Maybe it is not right way, but in my project i just increased user email size with south. Sample:

    >> ./manage.py schemamigration auth --initial && ./manage migrate auth --fake

Then i added into models.py:

    from django.contrib.auth.models import User
    field = User._meta.get_field('email')
    field.max_length = 254
    field = User._meta.get_field('username')
    field.max_length = 254

Now:

    >> ./manage.py schemamigration auth --auto
    >> ./manage.py migrate auth
Ildus
  • 143
  • 8
  • 2
    a tip: on settings.py add SOUTH_MIGRATION_MODULES = { 'auth': 'migrations', } and add a folder migrations with __init__.py, or else south will create the migrations inside the django installation, you will want this in your project folder and vcs – tovmeod Jun 05 '13 at 13:32