不同数据库上的Django身份验证 [英] Django Authentication on different databases
问题描述
我想为客户创建一个应用程序,其中每个客户都有自己的数据库.因此,作为登录信息,他们需要输入三个不同的字段:客户编号,用户名,密码.
I want to create an App for Customers where every customer has its own DB. So as Login information they need to enter three different fields: customernumber, username, password.
用户名和密码应该做普通的身份验证工作,customernumber可以转到正确的数据库用户表进行身份验证,我可以通过using()方法访问其他数据库,但是如何通过正确的方法对用户进行身份验证数据库?
The username and password should do the normal authentication stuff and the customernumber is there to go to the right database user table for authentication i can go to other databases through the using() method but how do you authenticate a user through the right database?
推荐答案
您应该像下面那样编写自定义身份验证后端,并且必须使用AUTHENTICATION_BACKENDS键在settings.py上指定它. Django文档
You should write custom authentication backend like below and it must be specified on settings.py using AUTHENTICATION_BACKENDS key. Django doc
from django.contrib.auth.backends import ModelBackend
class CustomAuthBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
user = User.objects.using(request.GET['CUSTOMER_TABLE']).filter(username=username)
if user.check_password(password) and self.user_can_authenticate(user):
return user
这篇关于不同数据库上的Django身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!