Created
June 10, 2016 13:37
-
-
Save fission6/1b0d409741db5a46c3bc74b940e1d5ba to your computer and use it in GitHub Desktop.
Django 1.9.6 Case Insensitive Username Logins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.functions import Lower | |
from django.db.models import CharField | |
from django.contrib.auth.models import UserManager as DjangoUserManager | |
CharField.register_lookup(Lower, 'lower') | |
class UserManager(DjangoUserManager): | |
""" | |
Allow case insensitive usernames for authenticating. | |
Use this Model Manager on your Custom User Model. | |
ie. objects = UserManager() | |
""" | |
def get_by_natural_key(self, username): | |
username_lower = username.lower() | |
return self.get( | |
username__lower=username_lower | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment