3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您應(yīng)該編寫一個(gè)自定義身份驗(yàn)證后端。這樣的事情會起作用:
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
class EmailBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
UserModel = get_user_model()
try:
user = UserModel.objects.get(email=username)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password):
return user
return None
然后,在您的設(shè)置中將該后端設(shè)置為您的auth后端:
AUTHENTICATION_BACKENDS = ['path.to.auth.module.EmailBackend']
已更新。繼承自ModelBackend它get_user()已經(jīng)實(shí)現(xiàn)的方法。
- 3 回答
- 0 關(guān)注
- 1088 瀏覽
添加回答
舉報(bào)