Longer username in Django 1.7
Asked Answered
F

3

6

I want to increase the length of the username in django from 30 to around 80, I know it may be duplicate question but the previous answers are not working, for example https://kfalck.net/2010/12/30/longer-usernames-for-django

this is for Django 1.2.

Did anyone try similar hack for Django>1.5 Thanks in advance

Ferraro answered 16/10, 2014 at 8:40 Comment(0)
H
1

In Django 1.5 and above, the recommended approach would be to create a custom user model. Then you can make the username field exactly as you want.

Herb answered 16/10, 2014 at 9:20 Comment(2)
Apart from using custom user model, is there any way to do that?Ferraro
I can't think of a better approach. Somebody else might have a different answer.Herb
K
1

I had the same problem few days ago. Finally, I ended just with cutting off first 30 characters of the (old) username (into the new database table), and adding a custom authentication backend that will check the email instead of user name. Terrible hack I know, and I'm planning to fix it as soon as I have some time. The idea is following:

I already have a model class that has one-to-one relation with djangos auth.User. I will add another field there called full_username.

class MyCustomUserModel(models.Model):
    user = models.OneToOneField(
            settings.AUTH_USER_MODEL, related_name="custom_user")
    full_username = models.CharField(max_length=80, ...)
    ...

Then, I'll add another custom authentication backend that will check this field as username. It would look something like this:

from django.contrib.auth.backends import ModelBackend

class FullUsernameAuthBackend(ModelBackend):
    def authenticate(self, username=None, password=None, **kwargs):
        UserModel = get_user_model()
        if username is None:
            username = kwargs.get(UserModel.USERNAME_FIELD)

        try:
            user = UserModel._default_manager.filter(custom_user__full_username=username)
            # If this doesn't work, will use (the second case):
            # user = MyCustomUserModel.objects.filter(full_username=username).user
        if user.check_password(password):
            return user
    except UserModel.DoesNotExist:
        # Adding exception MyCustomUserModel.DoesNotExist in "(the second case)"
        # Run the default password hasher once to reduce the timing
        # difference between an existing and a non-existing user (#20760).
        UserModel().set_password(password)

After this, you need to change settings.py:

AUTHENTICATION_BACKENDS = (
    "....FullUsernameAuthBackend",
    # I will have the email auth backend here also.
)

I hope that it will work.

Knorr answered 16/10, 2014 at 9:39 Comment(2)
Note AUTHENTICATION_BACKENDS = (, so the original django's auth backend shouldn't be used (and auth.User.username should be disregarded on login in this case).Knorr
Actually I was taking email from user as username and storing it in both username field and email field, and was using default authentication system. Thanks for your answer.Ferraro
I
0

Custom User Models are a huge change to make and aren't always compatible with apps. I solved it by running this very pragmatic migration. Note this only solves it at the database level.

migrations.RunSQL("alter table auth_user alter column username type varchar(254);")

Interjacent answered 23/6, 2015 at 15:24 Comment(1)
How would you go about overriding auth_user form validations?Dingo

© 2022 - 2024 — McMap. All rights reserved.