class CustomAuth(ModelBackend): def authenticate(self, request, username=None, password=None, **kwargs): try: user = UserProfile.objects.get(Q(username=username) | Q(mobile=username)) if user.check_password(password): return user except Exception as e: return None
AUTHENTICATION_BACKENDS = [ 'apps.users.views.CustomAuth', # 引用自定义的认证后端 ]
官方文档:https://docs.djangoproject.com/en/2.2/topics/auth/customizing/