Longer username in Django 1.7

I want to increase the length of the username in django from 30 to 80, I know this may be a repeating question, but the previous answers do not work, for example https://kfalck.net/2010/12/30/longer-usernames-for- django

This is for Django 1.2.

Has anyone tried a similar hack for Django> 1.5 Thanks in advance

+5
source share
3 answers

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

0
source

I had the same problem a few days ago. Finally, I ended up just disconnecting the first 30 characters of the (old) username (in the new database table) and adding my own authentication server that will check email instead of the username. A terrible hack that I know, and I plan to fix it as soon as I have time. The idea is as follows:

I already have a model class that has a one-to-one relationship with djangos auth.User. I will add another field 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 will add another user authentication server that will check this field as a username. It will 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 that you need to change settings.py:

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

I hope this works.

0
source

User user models are a huge change that is not always compatible with applications. I solved this by conducting this very pragmatic migration. Note that this permits only at the database level.

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

0
source

Source: https://habr.com/ru/post/980834/


All Articles