在django中使用电子邮件而不是登录名 [英] Using email instead of login name in django

查看:192
本文介绍了在django中使用电子邮件而不是登录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这不是如何在电子邮件/密码对上进行身份验证的问题,而是如何产生逻辑的,如果你喜欢,美丽的数据结构。



我想在给定的django项目中使用电子邮件作为用户名。但是,由于至少有两个原因我无法重新使用auth.User模型提供的字段:


  1. auth.User .username的字段max_length是30个字符,这可能不足够用于某些电子邮件地址。


  2. auth.User.email不是唯一的 - 这显然是不满意的前提条件是用户名必须是唯一的。


这里一个明显的方法是存储用户名在自定义配置文件中,它与auth.User相关联。在这种情况下,我们必须处理以下问题:


  1. 为auth.User.username生成唯一的用户名 - 电子邮件的md5哈希应该很好这里?

  2. 完全取消auth.User.email为空 - 因为它只有75个字符长,而根据RFC 5321(有效电子邮件地址的最大长度是多少?)电子邮件可以长达256个字符。

以下问题源于提出的解决方案:


  1. 一个不是将能够重用内置的视图/模板以进行标准操作,如密码重置等。

  2. 如果电子邮件更改auth.User.username将必须更新

为了在火中添加石油,django开发人员在任何可预见的将来都不太可能解决这个限制 - 参见 http://code.djangoproject.com/ticket/11365



所以问题是:有没有其他方法可以做到你在上面提出的解决方案中还有其他的缺点吗?



谢谢!

解决方案>

我有一个自1995年以来一直在上市的商业网站的客户(是的,我们在这里说早期采用者)。无论如何,他们已经建立了一个已建立的用户群,并且这些名称完全符合Django用户名的想法。



我看了一下几乎没有办法处理它们,他们都感觉像黑客(这是2007年的夏天),所以我说拧了它,并直接侵入contrib.auth.models.User。我只需要修改大约10行代码,增加字段大小,并调整验证器。从那时起,我们已经进行了两次升级 - 0.97-pre => 1.0,1.0 => 1.1.1 - 每次只需要15分钟左右移植黑客。



这不是漂亮的,我可以在地狱里烧这样做,但是这样做的时间比其他任何事情都要花费更少的时间,而转发端口已经是一个总计非问题。


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.

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:

  1. auth.User.username 's field max_length is 30 characters, which might not be enough for some email addresses.

  2. auth.User.email is not unique - which is obviously not satisfactory for a prerequisite saying that user names have to be unique.

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:

  1. Generate unique username for auth.User.username - md5 hash of email should be fine here?
  2. Leave out completely auth.User.email empty - since it's only 75 characters long, while according to RFC 5321 (What is the maximum length of a valid email address?) email can be as long as 256 characters.

The following problems stem from the proposed solution:

  1. One is not going to be able to reuse built-in views/templates for standard operations like password reset etc
  2. In case of email change auth.User.username will have to be updated

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?

Thanks!

解决方案

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.

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".

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆