Django Authentication on different databases

你离开我真会死。 提交于 2019-12-11 05:13:44

问题


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.

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?


回答1:


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


来源:https://stackoverflow.com/questions/52836068/django-authentication-on-different-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!