Django Database routing based on current user logged in

后端 未结 3 2024
悲哀的现实
悲哀的现实 2020-12-11 08:39

In a view class, you could call self.request.user and perform actions based on that. In my case, I would like to be able to switch databases depend

3条回答
  •  自闭症患者
    2020-12-11 09:16

    To resolve this, I created a new model with just one userid field. Then in router when specifying db for read and write, I get the data from that model so I don't need any request argument.
    And I just added a short ajax code in my base template so whenever my website is opened it gets the userid from request.user and change the model row so current user id is changed like so.

提交回复
热议问题