我可以将 Django 的 auth_user.username 字段更改为 100 个字符长而不破坏任何内容吗?

发布于 2024-10-14 11:23:54 字数 632 浏览 7 评论 0原文

在有人将此问题标记为此问题的重复之前 django的auth_user.username可以是varchar(75)吗?这是怎么做到的?或其他类似的问题,请阅读这个问题。我链接到的问题准确地提出了这个问题,但不幸的是,答案没有解决所提出的问题。

我可以通过执行以下操作将 auth_user.username 字段更改为 100 个字符长:

  1. 在 DB 中为用户名字段运行 ALTER table
  2. 更改此处的 max_length: username = models.CharField(_('username'), max_length= 30, unique=True, help_text=_("必需。30个字符或更少。字母、数字和@/./+/-/_字符"))

如果我这样做,它会破坏 Django 中的任何内容吗这?

当我将 Django 更新到更高版本时,这会中断,这不是问题。我也不考虑编写其他身份验证方法。我只是想知道如果我这样做是否会破坏任何东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

水中月 2024-10-21 11:23:54

您需要在几个地方对 max-length 进行猴子修补:模型字段的描述、表单字段的描述和最大长度验证器。最大长度验证器附加到表单字段和模型字段。

这是一个代码片段,它将修补所有内容:

https://gist.github.com/1143957 (使用 django 1.2 和 1.3 进行测试)

更新:
好消息!从 django 1.5 开始,您可以覆盖用户模型: 自定义用户模型

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

素罗衫 2024-10-21 11:23:54

这样做并没有什么坏处。
只需更改模型和数据库中的长度:)

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

我做我的改变 2024-10-21 11:23:54

创建一个用户配置文件模型,在其中添加很长的用户名字段,然后使用它。当然,这会使真正的用户名字段变得无用,但它比破解 Django 代码要好得多,后者会在您需要升级它时给您带来麻烦。

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.

池木 2024-10-21 11:23:54

对我来说,下面的代码有效并且很简单。

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    ....
    # your custom fields

MyUser._meta.get_field('username').max_length = 255  # or 100

在您可以运行迁移之后

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

り繁华旳梦境 2024-10-21 11:23:54

如果您手动更改数据库以及相应的模型,那么应该没有问题。

您也可以改回来,我会说进行备份以防万一,但我不确定是否有必要

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

挽心 2024-10-21 11:23:54

对于未来的需求,这是我发现的最好、最简单的方法:

https://github.com/GoodCloud /django-longer-用户名

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

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

毁虫ゝ 2024-10-21 11:23:54

另一个解决方案是更改您的身份验证代码。当新用户注册时,他们输入电子邮件,您将其保存在电子邮件字段中,以便您拥有它,如果他们的电子邮件超过 30 个字符,您可以在将其放入用户字段之前缩短它。然后,更改新登录的登录身份验证。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文