消除 Django 用户模型 30 个字符限制的最简单方法是什么?
我继承了一个使用自定义无表单身份验证后端的 Django 应用程序。它通过 SQL hack ALTER TABLE auth_user MODIFY COLUMN username varchar(90);
来解决 django.contrib.auth.models.User
中的 30 个字符限制,其内容为当然只对数据库有效,对表单无效。由于我试图消除代码中的所有 hack(包括 SQL 代码),因此我正在寻找一种适当的方法来消除该限制。您建议如何删除它?
Possible Duplicate:
Can django's auth_user.username be varchar(75)? How could that be done?
I've inherited a Django application which uses a custom form-less auth backend. It works around the 30 character limit in django.contrib.auth.models.User
by a SQL hack ALTER TABLE auth_user MODIFY COLUMN username varchar(90);
, which is of course only valid for the database, but not for forms. Since I'm trying to remove all the hacks from code including SQL ones, I'm looking for a proper way to remove that limitation. How would you recommend removing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想不出任何干净的方法来对 django.contrib.auth.models.User 模型进行这样的更改。事实上,几乎任何改变都是棘手的。
一种非常不稳定的方法是将用户名委托给 UserProfile 模型。您可以定义与
User
具有一对一关系的UserProfile
模型。然后,您必须向配置文件模型添加一个字段来保存比平常更长的用户名。事情还没有结束。为了使事情无缝进行,您必须编写自定义管理器以确保在查询用户时使用配置文件字段中的用户名。
既然我已经把它说出来了,我根本不喜欢上面的方法。不用说它未经测试。
I can't think of any clean way to make such a change to the
django.contrib.auth.models.User
model. In fact almost any change is tricky.One very flaky way would be to delegate the username to an
UserProfile
model. You can define anUserProfile
model that has a one-to-one relationship withUser
. You'll then have to add a field to the profile model to hold the longer-than-usual username.It doesn't end there. To make things seamless you'll have to write custom managers to make sure that the username from the profile field is used when querying for users.
Now that I have spelled it out I don't like the above method at all. Needless to say it is untested.