I’m trying to find the logged in user in a different context of the views. In this specific case, I’m trying to manage a database routing according to logged user. Is there any way to discover the logged in user instead of ‘request’ object in views? (Or any way to pass / view the request object in a database routing file).
Thanks in advance!
This isn’t something that would be universally valid - there are multiple contexts in which database routing can be done and not under the context of a view. (System initialization, migrations, and
manage.py commands to name three.) So you’re not always going to have a request that can be supplied to the router.
Instead, I would recommend making this type of decision in your views. If you’re doing this a lot, I’d look at some type of utility class or function that can be called from your views to reduce duplication of code. (The exact mechanism for this would depend upon the use of Class-based views vs function-views)