简体   繁体   English

Django用户模型用户名(唯一= False)

[英]Django user model username (unique=False)

I have seen many of the solution but none of it seems to be answering the question. 我见过许多解决方案,但似乎都没有回答这个问题。

My user model is using AbstractUser right now. 我的用户模型现在正在使用AbstractUser。 Is there a way to make username(unique = False) 有没有办法建立用户名(唯一=假)

Since i wanted to allow user to ba able to have duplication of same username, as the login im am using is by email address and password 因为我想允许用户能够复制相同的用户名,因为我使用的登录是通过电子邮件地址和密码

Below is the code that i tried but doesnt work. 下面是我尝试但不起作用的代码。

Code: 码:

class MyUser(AbstractUser):
    username = models.CharField(max_length=30, unique=False)

error: 错误:

customuser.MyUser: (auth.E003) 'MyUser.username' must be unique because it is named as the 'USERNAME_FI ELD'

Try to specify email as username field with USERNAME_FIELD attribute: 尝试使用USERNAME_FIELD属性将电子邮件指定为用户USERNAME_FIELD

class MyUser(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    USERNAME_FIELD = 'email'
class MyUser(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    email = models.EmailField(max_length=255, unique=True)
    USERNAME_FIELD = 'email'

A non-unique username field is allowed if you use a custom authentication backend that can support it. 如果您使用可支持它的自定义身份验证后端,则允许使用非唯一的用户名字段。

If you want to use django's default authentication backend you cannot make username non unique. 如果要使用django的默认身份验证后端,则无法使用户名非唯一。

You will have to implement a class with get_user(user_id) and authenticate(request, **credentials) methods for a custom backend. 您必须使用get_user(user_id)实现一个类,并为自定义后端实现身份验证(请求,**凭据)方法。

You can read it in the documentation here. 您可以在此处的文档中阅读它。 https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#specifying-custom-user-model https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#specifying-custom-user-model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM