扩展 Django 用户模型的合理策略?
这看起来应该没问题,因为我没有触及用户模型本身。
我想纯粹根据电子邮件和密码来注册用户。用户模型中需要用户名。我的想法是,我可以创建一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此处有一些答案。
似乎最好的方法可能是编写 自定义auth 后端(不太难)接受电子邮件地址登录,并将电子邮件的哈希值存储在用户名字段中,以满足所需的唯一用户名。
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.
关闭。
使用
clean
方法将电子邮件地址复制到用户名。除了默认登录表单之外,这对于所有内容都非常有效。需要调整默认登录表单以允许用户名是电子邮件地址。默认形式仅需要字符,并且拒绝标点符号。
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.