6

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. 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'

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
Jin Nii Sama
  • 707
  • 1
  • 16
  • 33

4 Answers4

3

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.

You will have to implement a class with get_user(user_id) and authenticate(request, **credentials) methods for a custom backend.

You can read it in the documentation here. https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#specifying-custom-user-model

himank
  • 439
  • 3
  • 5
2

Try to specify email as username field with USERNAME_FIELD attribute:

class MyUser(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    USERNAME_FIELD = 'email'
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
2
class MyUser(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    email = models.EmailField(max_length=255, unique=True)
    USERNAME_FIELD = 'email'
Nikhil Bhardwaj
  • 562
  • 10
  • 18
  • Please explain a little about your answer. – Mehrdad Pedramfar Feb 16 '19 at 07:45
  • @MehrdadPedramfar I don't know about his code so I just want give some info related to his code and I don't want to add something additional in it..... And he is inheriting AbstractUser class so, that username field will directly make an impact on the Django username field.... This is it – Nikhil Bhardwaj Feb 16 '19 at 10:45
1

It is possible, if a custom user class is used and with the authentication backend from Django itself:

  1. Create a custom user class based on the Abstract User class:

     from django.contrib.auth.models import AbstractUser
    
     class CustomUser(AbstractUser):
         username = models.CharField(_("username"), max_length=254, unique=False)
         email = models.EmailField(_("email"), max_length=254, unique=True)
    
         USERNAME_FIELD = "email"
         REQUIRED_FIELDS = ["username"]
    
         objects = CustomUserManager()
    
         def __str__(self):
           return self.username
    
  2. Create a Custom User Manager class, as described in: https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#writing-a-manager-for-a-custom-user-model

  3. Update the SQL database configuration by running in bash:

     manage.py makemigrations
     manage.py migrate
    

The username field now should be recognised as non-unique, while the email is the unique key identifier.

Hope this helps!

MK.