在Django中使用电子邮件,而不是登录名 [英] Using email instead of login name in django
问题描述
首先,这不是问题,如何在电子邮件/密码认证对,而是如何产生的逻辑,如果你喜欢,漂亮的数据结构。
Firstly, this is not the question how to authenticate on email/password pair, but rather how to produce logical, and if you like, beautiful data structure.
我要使用电子邮件作为用户名在给定的Django项目。然而,我无法再使用的auth.User模式至少有两个原因提供的字段:
I want to use emails as user names in a given django project. However, I am unable to re-use fields provided by auth.User model for at least two reasons:
-
auth.User.username的领域MAX_LENGTH为30个字符,这可能是不够的一些电子邮件地址。
auth.User.username 's field max_length is 30 characters, which might not be enough for some email addresses.
auth.User.email不是唯一的 - 这显然是不能令人满意的prerequisite说,用户名必须是唯一的。
auth.User.email is not unique - which is obviously not satisfactory for a prerequisite saying that user names have to be unique.
所以这里一个明显的方式是存储的用户名在自定义配置文件,这是与auth.User。在这种情况下,我们不得不面对以下问题:
So an obvious way here is to store username in a custom profile, which is linked to auth.User. In this case we have to deal with following problems:
- 生成的auth.User.username唯一的用户名 - 电子邮件的MD5哈希值应该是在这里很好 ?
- 将出完全auth.User.email空的 - 因为它只有75个字符长,根据RFC 5321(当<一href=\"http://stackoverflow.com/questions/386294/maximum-length-of-a-valid-email-id\">http://stackoverflow.com/questions/386294/maximum-length-of-a-valid-email-id)电子邮件可以长达256个字符。
- Generate unique username for auth.User.username - md5 hash of email should be fine here?
- Leave out completely auth.User.email empty - since it's only 75 characters long, while according to RFC 5321 (http://stackoverflow.com/questions/386294/maximum-length-of-a-valid-email-id) email can be as long as 256 characters.
下面的问题从拟议的解决办法干:
The following problems stem from the proposed solution:
- 一个是不会能够重用像密码标准操作内置视图/模板复位等
- 在电邮变化auth.User.username的情况下,将不得不更新
要加机油投入火中,Django开发不可能解决任何可预见的未来这种限制 - 见的 HTTP://$c$c.djangoproject.com/ticket/11365
To add oil into the fire, django developers are not likely to fix this limitation in any foreseeable future - see http://code.djangoproject.com/ticket/11365
所以,问题是:是否有任何其他的方式来做到这一点,你看到上面提出的解决方案的任何其他缺点
So the question is: is there any other way to do it and do you see any other drawbacks in the solution proposed above?
谢谢!
推荐答案
我有一个商业网站自1995年以来(是的,我们在这里谈论尝鲜),已经在一个客户端。无论如何,他们已经有了一个既定的用户群和名字分别的完全的不符合用户名Django的想法。
I had a client with a commercial site that had been up since 1995 (yeah, we're talking early adopters here). Anyway, they already had an established user base and the names were totally non-compliant with Django's idea of a username.
我看了一些方法来处理它,他们都觉得像黑客(这是2007年夏),所以我说拧,直接砍死contrib.auth.models.User。我只有改变约10 code的线条,增加字段的大小,并调整了验证。从那时起,我们已经做了两次升级 - 0.97- pre => 1.0和1.0 => 1.1.1 - 而这只是拍摄,每次约15分钟,以口黑客
I looked at a few ways to handle it and they all felt like hacks (this was Summer of 2007), so I said screw it and hacked contrib.auth.models.User directly. I only had to change about 10 lines of code, increase the field size, and tweak the validator. We've done two upgrades since then -- 0.97-pre => 1.0, and 1.0 => 1.1.1 -- and it's only taken about 15 minutes each time to "port the hack".
这不是pretty,我会在地狱里燃烧做类似这样的,但它花更少的时间来做到这一点比什么都这样,我可以找出和转发端口是一个总的非-issue。
It isn't pretty, and I may burn in Hell for doing it like this, but it took less time to do it this way than anything else I could figure out and the forward ports have been a total non-issue.
这篇关于在Django中使用电子邮件,而不是登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!