8

Before somebody marks this question as a duplicate of this question Can django's auth_user.username be varchar(75)? How could that be done? or other such questions on SO, please read this question. The question I linked to asks this question precisely but unfortunately the answers don't address the question that was asked.

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

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

Would it break anything in Django if I were to do this?

That this will break when I update Django to a higher version is not a problem. I'm also not looking at writing other authentication methods.I just want to know if I would break anything if I were to do this.

Community
  • 1
  • 1
Arpit Rai
  • 1,017
  • 1
  • 13
  • 26

7 Answers7

20

You need to monkey-patch max-length in several places: model-field's description, form-field's description and max-length validators. Max-length validators are attached to form-fields as well as model-fields.

Here is a code snippet, which will patch everything:

https://gist.github.com/1143957 (tested with django 1.2 and 1.3)

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

Ski
  • 14,197
  • 3
  • 54
  • 64
  • +1 Nice one. The other post explains this process but not **how** you'd have to override default django forms validation. – Yuji 'Tomita' Tomita Jan 28 '11 at 19:31
  • 3
    Also, I added 3 lines to let users register with 75 characters: from django.contrib.auth.models import User User._meta.get_field_by_name('username')[0].max_length=75 User._meta.get_field('username').validators[0].limit_value = 75 – Thomas Aug 13 '11 at 01:20
1

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 you can run your migration

Mateus Padua
  • 183
  • 2
  • 8
1

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

Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41
1

Create a user profile model, add your very-long-username field there, and use it. of course this renders the genuine username field useless, but it is much better than hacking Django code, which will get you into trouble when you need to upgrade it.

shanyu
  • 9,536
  • 7
  • 60
  • 68
0

for future needs this is the best and easiest way i found out:

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

pedrotorres
  • 1,222
  • 2
  • 13
  • 26
0

Another solution is to change your authentication code. When a new user signs up, they enter an email, you save this in the email field so you have it, and if their email is >30 characters, you shorten it before putting it into the user field. Then, change your login authentication for new logins.

Pstrazzulla
  • 455
  • 5
  • 6
0

If you change the database manually as well as the model accordingly then there should be no problem.

You can also change back otherwise, and I would say make a backup just in case but I'm not sure its even necessary

zenna
  • 9,006
  • 12
  • 73
  • 101