1

My users are using e-mail addresses as usernames, which caused a problem in Django 1.9 because there was a 30 character maximum username length.

This was supposed to be fixed by Django 1.10, with a 150 character limit, I believe (though it looks like the docs actually say that max_length should be able to go up to 191 characters for certain databases).

I upgraded to Django 1.11 but username still seems to be limited to 30 characters. Is it because I need to set the value of max_length somewhere? If so, where do I set this (an example would be great)? If not, how do I increase the max length?

Here is my current view.

def register(request):
    alreadyRegistered = False
    invalidID = False
    context_dict = {}
    if request.method == 'POST':
        # Gather the username and password provided by the user.
        # This information is obtained from the login form.
        Userid = request.POST['username']
        Userid = Userid[0:29] # horrible work-around that has to change
        if User.objects.filter(username=Userid).count() > 0:
            alreadyRegistered = True
        else:
            isGU = 1 > 0 # this will always = TRUE for now
            if isGU or isEA:
                firstname = request.POST['firstname']
                lastname = request.POST['lastname']
                email = Userid
                password = request.POST['password']
                user = User.objects.create_user(Userid, email, password)
                user.first_name = firstname
                user.last_name = lastname
                user.save()
                registered = True
                user = authenticate(username=Userid, password=password)
                login(request, user)
                return HttpResponseRedirect('/studentprogress/')
            else:
                invalidID = True

    context_dict['alreadyRegistered'] = alreadyRegistered
    context_dict['invalidID'] = invalidID
    return render(request,
            'studentprogress/register.html', context_dict)
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • 1
    Possible duplicate of [Override default django username validators for AbstractUser model](https://stackoverflow.com/questions/29921900/override-default-django-username-validators-for-abstractuser-model) – Javier Campos Jun 07 '17 at 20:16
  • I think this is a duplicate, maybe look here https://stackoverflow.com/questions/29921900/override-default-django-username-validators-for-abstractuser-model – Javier Campos Jun 07 '17 at 20:17
  • So I have to create a new user model and then use that and override the length there basically? – Hack-R Jun 07 '17 at 20:20

2 Answers2

1

One way to allow custom length of username field is to use custom user model. The documentation is here: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#specifying-a-custom-user-model

Extend your custom user model from AbstractBaseUser which provides necessary methods to work with Django authentication.

Basically you can just create the model with data you need to have and define the required fields in the model. In your case you might want to set the USERNAME_FIELD and EMAIL_FIELD to be the same field in model.

Pettis
  • 130
  • 4
0

Django 1.10 shipped with a migration to alter the username max_length. You just have to run migrations i.e. python manage.py migrate auth to update your database tables.

Jeff
  • 1,002
  • 8
  • 9