Search code examples
pythondjangopython-3.xdjango-authenticationdjango-login

Login with both username and password in django 2.2.5


How can we login with username or password in Django site and Django Admin pages?

I tried by creating custom user model and making email field as username field but I can don't want to login with either username or email.

I want to use any of both any time to login.


Solution

  • You can use { from django.db.models import Q

    Example:

    from django.db.models import Q
    user = "username/eamil"
    password= "your password"
    user_check = models.YourModelName.objects.filter((Q(email = user)|Q(username = user)), password = password)
    

    }