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

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

问题描述

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



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


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


  2. auth.User.email不是唯一的 -


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


  1. 为auth.User.username生成唯一的用户名 - md5电子邮件哈希应该很好
  2. / 386294 / maximum-length-of-a-valid-email-id>有效电子邮件地址的最大长度是多少?)电子邮件长度可达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天全站免登陆