title: how to login a user with email or username in django custom user model
body: <p>This is my current code can anyone please add the required code to allow a user to login with email or username</p>
  <blockquote>
  <p>backends.py</p>
  </blockquote>
  <pre><code>from django.contrib.auth import get_user_model
  from django.contrib.auth.backends import ModelBackend, UserModel
  
  class CaseInsensitiveModelBackend(ModelBackend):
      
      def authenticate(self, request, username=None, password=None, **kwargs):
          UserModel = get_user_model()
          if username is None:
              username = kwargs.get(UserModel.USERNAME_FIELD)
          
          try:
              case_insensitive_username_field = '{}__iexact'.format(UserModel.USERNAME_FIELD)
              user = UserModel._default_manager.get(**{case_insensitive_username_field: username})
          except UserModel.DoesNotExist:
              UserModel().set_password(password)
  
          else:
              if user.check_password(password) and self.user_can_authenticate(user):
                  return user
  
  </code></pre>
  <blockquote>
  <p>custom user models</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/f9Rmw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f9Rmw.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/uxkte.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uxkte.png" alt="enter image description here" /></a></p>