1 class ModelBackend(object): 2 """ 3 Authenticates against settings.AUTH_USER_MODEL. 4 """ 5 6 def authenticate(self, request, username=None, password=None, **kwargs): 7 if username is None: 8 username = kwargs.get(UserModel.USERNAME_FIELD) 9 try: 10 user = UserModel._default_manager.get_by_natural_key(username) 11 except UserModel.DoesNotExist: 12 # Run the default password hasher once to reduce the timing 13 # difference between an existing and a non-existing user (#20760). 14 UserModel().set_password(password) 15 else: 16 if user.check_password(password) and self.user_can_authenticate(user): 17 return user
红色部分大概依稀感觉到是什么意思,没深入研究,有空看看!