使用 Django 用户管理使电子邮件字段唯一
有一个几乎类似的问题: 如何使 Django 中的 contrib.auth 用户模型中的电子邮件字段唯一
该解决方案并不完美:验证电子邮件的唯一性。提供的解决方案相当有趣。它不允许对用户进行修改而使电子邮件保持完整。如何修复它?提前致谢!
There was a nearly similar question: How to make email field unique in model User from contrib.auth in Django
The solution was not perfect: Validating email for uniqueness. The solution provided is rather funny. It disallows modifications to User that leave email intact. How to fix it? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的 __init__.py 中
in your
__init__.py
感谢奥弗里·拉维夫,但我所看到的并不是我需要的。所以我解决了自己的问题,现在我想分享一下提示:
使用用户名而不是电子邮件,从表单中排除电子邮件。将其标签屏蔽为电子邮件。
子类化用户并创建一个接收电子邮件地址的唯一字段,将其掩码为电子邮件,从表单中排除原始电子邮件字段。
子类化用户并创建
就这么简单,但花了我一些时间。希望它能帮助其他有同样需要的人。
Thanks to Ofri Raviv but what I've seen is not what I needed. So I resolved my own issue and now I'd like to share the tips:
Use username instead of email, exclude email from the form. Mask its label as email.
Subclass User and create a UNIQUE field which receives email addresses, mask it as email, exclude original email field from the form.
It's that simple but took me some time. Hope it help others with the same need.
此方法不会使电子邮件字段在数据库级别上唯一,但值得尝试。
使用自定义 validator:
然后在 forms.py 中:
This method won't make email field unique at the database level, but it's worth trying.
Use a custom validator:
Then in forms.py: