Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change Django's auth_user.username field to be 100 chars long without breaking anything?

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.

like image 285
Arpit Rai Avatar asked Jan 28 '11 12:01

Arpit Rai


People also ask

How can I change user field in Django?

As we all know the default User model in django uses a username to uniquely identify a user during authentication. If you need to replace this username field with other field lets say an email field then you need to create a custom user model by either subclassing AbstractUser or AbstractBaseUser.

What is username field in Django?

For Django's default user model, the user identifier is the username, for custom user models it is the field specified by USERNAME_FIELD (see Customizing Users and authentication). It also handles the default permissions model as defined for User and PermissionsMixin .

What is Auth_user_model?

AUTH_USER_MODEL is the recommended approach when referring to a user model in a models.py file. For this you need to create custom User Model by either subclassing AbstractUser or AbstractBaseUser.

What is custom user model?

Custom user model manager where email is the unique identifiers. for authentication instead of usernames. """ def create_user(self, email, password, **extra_fields):


1 Answers

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

like image 199
Ski Avatar answered Oct 13 '22 20:10

Ski