Can I change the Django auth_user.username field to 100 characters without breaking anything?

Before anyone marks this question as a duplicate of this question Can django auth_user.username be varchar (75)? How can I do that? or other similar questions on SO, please read this question. The question I asked poses exactly this question, but, unfortunately, the answers do not affect the question that was asked.

Can I change the auth_user.username field to 100 characters by doing the following:

  • Run the ALTER table in the database for the username field
  • Change max_length here: username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))

Will it really break anything in Django if I do this?

To make this change when I upgrade Django to a higher version, this is not a problem. I also do not look at the use of other authentication methods. I just want to know if I will break something if I do this.

+7
source share
7 answers

In several places, the maximum length of the monkey patch is required: a description of the model field, a description of the form field, and validators of the maximum length. Validators of maximum length are attached to the fields of the form, as well as to the fields of the model.

Here is a piece of code that will fix everything:

https://gist.github.com/1143957 (verified using django 1.2 and 1.3)

Update: Good news! With django 1.5 you can override the user model: Customizing the user model

+20
source

There is no harm. Just change the length in the model and in the database :)

+1
source

Create a user profile model, add a field with a very long username to it and use it. of course, this makes the real username field useless, but it's much better than hacking Django code, which will lead to trouble when you need to update it.

+1
source

For me, the code below works and is simple well.

 from django.contrib.auth.models import AbstractUser class MyUser(AbstractUser): .... # your custom fields MyUser._meta.get_field('username').max_length = 255 # or 100 

after starting the migration

+1
source

If you change the database manually, as well as the model accordingly, then there should not be any problems.

You can also reverse otherwise, and I would say back up just in case, but I'm not sure if it’s even necessary

0
source

for future needs, this is the best and easiest way to find out:

https://github.com/GoodCloud/django-longer-username

0
source

Another solution is to change the authentication code. When a new user subscribes, they enter an email, you save it in the email field so that you have it, and if his email address is> 30 characters, you shorten it before putting it in the user field. Then change the login authentication for new entries.

0
source

All Articles