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

杀马特。学长 韩版系。学妹 提交于 2019-12-03 13:59:12

问题


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.


回答1:


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




回答2:


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




回答3:


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.




回答4:


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




回答5:


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




回答6:


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

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




回答7:


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.



来源:https://stackoverflow.com/questions/4827965/can-i-change-djangos-auth-user-username-field-to-be-100-chars-long-without-brea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!