Multiple USERNAME_FIELD in django user model

Saurabh Verma

My custom user model:

class MyUser(AbstractBaseUser):
    username = models.CharField(unique=True,max_length=30)
    email = models.EmailField(unique=True,max_length=75)
    is_staff = models.IntegerField(default=False)
    is_active = models.IntegerField(default=False)
    date_joined = models.DateTimeField(default=None)

    # Use default usermanager
    objects = UserManager()

    USERNAME_FIELD = 'email'

Is there a way to specify multiple USERNAME_FIELD ? Something like ['email','username'] so that users can login via email as well as username ?

Alasdair

The USERNAME_FIELD setting does not support a list. You could create a custom authentication backend that tries to look up the user on the 'email' or 'username' fields.

from django.db.models import Q

from django.contrib.auth import get_user_model

MyUser = get_user_model()

class UsernameOrEmailBackend(object):
    def authenticate(self, username=None, password=None, **kwargs):
        try:
           # Try to fetch the user by searching the username or email field
            user = MyUser.objects.get(Q(username=username)|Q(email=username))
            if user.check_password(password):
                return user
        except MyUser.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a non-existing user (#20760).
            MyUser().set_password(password)

Then, in your settings.py set AUTHENTICATION_BACKENDS to your authentication backend:

 AUTHENTICATION_BACKENDS = ('path.to.UsernameOrEmailBackend,)\

Note that this solution isn't perfect. For example, password resets would only work with the field specified in your USERNAME_FIELD setting.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django: Customize the User model's return field

Django get user by generic USERNAME_FIELD

Django model field set default user in model

Django custom User model throwing SystemCheckError - The field 'username' clashes with the name 'username'

Omitting password field for custom django user model

Django - Take username of logged user as default value to another model

How do I add username of logged in user to model field in django

How to store multiple values on a django model field

Storing multiple strings to a single field in Django model

Compare Django model field and request.user.username in HTML

How to associate a username from the User model to another model in Django?

Add a field to the Django admin for a custom user model

Django admin model field set to current user

How to correctly use multiple user model in django?

Django extending user model

Django Custom User Model

Django saving in a model with User field

In Django 1.8, what does "USERNAME_FIELD" mean by authentication system?

django model - fetching user data accross multiple tables

Accessing a field on a Django model that is linked to a different model via the User model

Django user model

Using model as multiple field of another model in Django

Adding data to a Django model associated with a user field

Django model owner field reading the log in user as None instead of the username

Custom User Model with multiple Unique id - Django RestFramework

How can I get a username from django User Model

How to change the field of username to user_name in to django custom user model?

How to properly set up custom User model and multiple databases with Django?

Ask model field only in creating user not super user. (Django)