title: Is there any way to login to Django admin interface using username or email?
body: <p>I want to implement  a login system to login to Django admin via email or username. Does anyone know how to implement it. I use custom user model. I know how to allow user to  login to website using username or email. But it doesn't work in Django admin interface</p>
  
  <pre><code>from django.conf import settings
  from django.contrib.auth import get_user_model
  
  
  class EmailOrUsernameModelBackend(object):
  
      def authenticate(self, username=None, password=None):
          if '@' in username:
              kwargs = {'email': username}
          else:
              kwargs = {'username': username}
          try:
              user = get_user_model().objects.get(**kwargs)
              if user.check_password(password):
                  return user
          except User.DoesNotExist:
              return None
  
      def get_user(self, username):
          try:
              return get_user_model().objects.get(pk=username)
          except get_user_model().DoesNotExist:
              return None
  </code></pre>