django auth 支持相同的用户名吗?
我有两个数据库,我必须将用户信息从这些数据库移至 django。问题是用户名重复(这些是其他用户)。又是如何解决的呢?
I have two databases and i must move users infor from these to django. The problem is that user names are repeated (these are other users). How does it solve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会“解决”多个用户名 -
django.contrib.auth.models.User.username
具有unique=True
因此每个用户名都必须是唯一的。当您尝试添加具有相同用户名的第二个用户时,数据库会通过抛出数据库
IntegrityError
来为您解决该问题。如果您有一个具有多个用户名的工作身份验证系统,那么您显然不会使用用户名作为用户的唯一标识符登录。
也许您正在使用电子邮件地址进行登录,用户名字段只是“天赋”(几乎像这样) - 在这种情况下,我可能会将用户名字段修改得更长,并将电子邮件地址存储在用户名字段/用户名中轮廓模型。
https://docs.djangoproject.com/ en/dev/topics/auth/#storing-additional-information-about-users
您还可以删除该唯一约束并编写自己的身份验证后端,该后端知道如何查找您的唯一用户/检查其凭据。每个用户一定有/某些/独特之处,对吧?
https://docs.djangoproject.com/en/ dev/topics/auth/#writing-an-authentication-backend
It doesn't 'solve' multiple user names -
django.contrib.auth.models.User.username
hasunique=True
so each one must be unique.The database would solve that problem for you by spitting out a database
IntegrityError
when you try to add a second user with the same username.If you have a working auth system with multiple usernames, then you're clearly not logging in with the username as a user's unique identifier.
Maybe you're using email addresses for login with the username field as just "flair" (almost like SO) - in which case I might modify the username field to be longer and store the email address in the username field / username in a user profile model.
https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
You could also potentially remove that unique constraint and write your own authentication backend that knows how to find your unique user / check its credentials. There must be /something/ unique about each user, right?
https://docs.djangoproject.com/en/dev/topics/auth/#writing-an-authentication-backend