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

查看:21
本文介绍了在 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:

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

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

auth.User.email 不是唯一的——这显然不能满足要求用户名必须唯一的先决条件.

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:

  1. 为 auth.User.username 生成唯一的用户名 - 电子邮件的 md5 哈希在这里应该没问题?
  2. 将 auth.User.email 完全留空 - 因为它只有 75 个字符长,而根据 RFC 5321 (有效电子邮件地址的最大长度是多少?) 电子邮件最长可达 256 个字符.
  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. 将无法重复使用内置视图/模板进行密码重置等标准操作
  2. 如果电子邮件发生更改,则必须更新 auth.User.username

火上浇油,django 开发人员不太可能在任何可预见的未来修复这个限制 - 参见 http://code.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 行代码,增加字段大小并调整验证器.从那时起,我们进行了两次升级——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".

它并不漂亮,我可能会因为这样做而在地狱中燃烧,但这样做所花费的时间比我能想到的任何其他方法都要少,而且转发端口完全不是问题.

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天全站免登陆