扩展 Django 用户模型的合理策略?

发布于 2024-10-25 10:27:17 字数 208 浏览 1 评论 0原文

这看起来应该没问题,因为我没有触及用户模型本身。

我想纯粹根据电子邮件和密码来注册用户。用户模型中需要用户名。我的想法是,我可以创建一个自定义 UserCreationForm,将电子邮件地址保存为用户名并验证电子邮件地址。

不幸的是,这样我就必须通过 user.username 来引用电子邮件地址。

这是标准吗?有更好的办法吗?

谢谢

This seems like it should be fine, since I'm not touching the User model itself.

I want to register users purely based on email and password. Username is required in the User model. My thought is that I can just create a custom UserCreationForm that saves email address AS username and validates the email address.

Unfortunately, with that, I'll have to refer to email address via user.username going forward.

Is this standard? Is there a better way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-11-01 10:27:17

Some answers here.

Seems like the best method may be to write a custom auth backend (not too difficult) to accept email addresses to log in, and store a hash of the email in the username field to satisfy the required, unique username.

安静 2024-11-01 10:27:17

我的想法是,我可以创建一个自定义 UserCreationForm,将电子邮件地址保存为用户名并验证电子邮件地址。

关闭。

使用 clean 方法将电子邮件地址复制到用户名。除了默认登录表单之外,这对于所有内容都非常有效。

需要调整默认登录表单以允许用户名是电子邮件地址。默认形式仅需要字符,并且拒绝标点符号。

My thought is that I can just create a custom UserCreationForm that saves email address AS username and validates the email address.

Close.

Use the clean method to copy email address to username. This works out pretty well for everything but the default login form.

The default login form needs to be tweaked to allow username to be an email address. The default form expects characters only, and rejects punctuation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文