Multiple Django apps, shared authentication

前端 未结 4 2002
遇见更好的自我
遇见更好的自我 2020-12-28 19:56

Two answers to this question, depending on whether sharing is across different sites or different subdomains Second answer: Multiple Django apps, shared authenticati

4条回答
  •  孤独总比滥情好
    2020-12-28 20:19

    You can use database routers for specifying which database should be used for auth backend.

    Here I have given a example router code below:

    class UserSessionRouter(object):
    
        def db_for_read(self, model, **hints):
            if model._meta.app_label == 'auth':
                return 'usersandsessions'
            elif model._meta.app_label == 'accounts':
                return 'usersandsessions'
            elif model._meta.app_label == 'sessions':
                return 'usersandsessions'
            return None
    
        def db_for_write(self, model, **hints):
            if model._meta.app_label == 'auth':
                return 'usersandsessions'
            elif model._meta.app_label == 'accounts':
                return 'usersandsessions'
            elif model._meta.app_label == 'sessions':
                return 'usersandsessions'
            return None
    

    Then specify router using the database setting DATABASE_ROUTERS and SESSION_COOKIE_DOMAIN as given below

    DATABASE_ROUTERS = ['site2.routers.UserSessionRouter']
    SESSION_COOKIE_DOMAIN = 'site1.com'
    

提交回复
热议问题