简体   繁体   English

Django,更改用户模型的用户名字段

[英]Django, alter username field of User model

I wish to add a maxlength of 255 characters for username field in django User model.我希望为 Django 用户模型中的用户名字段添加 255 个字符的最大长度。 I would also like to increase the maxlength of first_name and last_name fields.我还想增加 first_name 和 last_name 字段的最大长度。 I have looked through lot many posts saw monkeypatching as an option but I am unable to understand it.我浏览了很多帖子,将monkeypatching 视为一种选择,但我无法理解。 I was wondering if its possible to write a custom migration file and apply it somehow.我想知道是否可以编写自定义迁移文件并以某种方式应用它。 Is it possible and if yes some guidance on how to achieve it would be great.是否有可能,如果有的话,一些关于如何实现它的指导会很棒。

Note: It might seem like its a duplicate but lot of posts that I have found on SO are about adding a new field which can be achieved using User.add_to_class() .注意:它可能看起来是重复的,但我在 SO 上发现的很多帖子都是关于添加一个新字段,该字段可以使用User.add_to_class()实现。 I intend to alter maxlength.我打算改变maxlength。

This is a "large" change required to your Django project, and probably something that should have been done at the beginning rather than the end.这是您的 Django 项目所需的“大”更改,并且可能应该在开始而不是结束时进行。 As someone that's experienced migrations breaking on User models on production systems I agree with @Burhan Khalid - leave the contrib.auth.models.User model as it is.作为在生产系统上的用户模型上经历过迁移中断的人,我同意@Burhan Khalid - 保持contrib.auth.models.User模型不变

First let's look at the warnings in the documentation, from Substituting a custom User model :首先让我们看一下文档中的警告,来自替换自定义用户模型

Changing this setting after you have tables created is not supported by makemigrations and will result in you having to manually fix your schema, port your data from the old user table, and possibly manually reapply some migrations. makemigrations 不支持在创建表后更改此设置,这将导致您必须手动修复架构、从旧用户表移植数据,并可能手动重新应用某些迁移。

I understand that you want to "just increase" the length of these fields, but this isn't a trivial change, or one that migrations was "meant" to deal with.我知道您想“增加”这些字段的长度,但这不是一个微不足道的变化,或者迁移是“打算”处理的。 The "Django-way" to solve this problem is to add a custom User model that fulfils the new requirements.解决这个问题的“Django 方式”是添加一个满足新要求的自定义 User 模型。 Unfortunately, this means that you'll have to handle a migration manually if you have existing user data on a production server.不幸的是,这意味着如果您在生产服务器上有现有的用户数据,您将不得不手动处理迁移。 The standard instructions on Customizing authentication in Django will help you out. 在 Django 中自定义身份验证的标准说明将帮助您。

The benefits you'll receive, which hopefully will pay off the hard work, are:您将获得的好处是:

  • Project code is protected against any upgrades.项目代码受到保护,不受任何升级的影响。 The default User model won't have any customisations.默认的 User 模型不会有任何自定义。
  • Project code will conform to the "Django-way" making it easier for other developers and team members to work with in the future.项目代码将符合“Django 方式”,方便其他开发人员和团队成员在未来工作。

I understand that's probably not what you want to hear, but I hope it helps.我知道这可能不是您想听到的,但我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将登录用户的用户名添加到 django 中的模型字段 - How do I add username of logged in user to model field in django 避免在自定义用户 model 中为 django allauth 创建用户名字段 - Avoid username field creation in custom user model for django allauth 用户模型中按用户名的Django queryset - Django queryset by username in user model 如何在不扩展用户 Model 的 Django ModelForm 中自定义默认用户 Model 的用户名字段的显示? - How do I customize the display of username field of default User Model in Django ModelForm without extending User Model? 如何在 django 自定义用户 model 中将用户名字段更改为 user_name? - How to change the field of username to user_name in to django custom user model? django模型无法将关键字“用户名”解析为字段 - django model Cannot resolve keyword 'username' into field Django 自定义模型 - 字段用户名被删除 - Django custom model - field username gets removed 从Django模板中单独模型上的user_id字段输出用户名 - Output username from user_id field on separate model in Django Template Django用户模型用户名(唯一= False) - Django user model username (unique=False) 从 model django 获取用户的用户名 - Grab username of User from model django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM