Reputation: 2085
I am using django App and i have several python modules in it.
When user post something then there is userid in it
userid = 234
From the post i have various modules doing their function.
Now in all those modules i want the userid
which i can use in log statement.
I really don't want to explicitly supply that id everywhere.
Is there any way i can set userid
at the start of post
and that gets available across all the modules i use for that request
Can i use something like this
tdata = threading.local()
tdata.userid = 234
Then in parser_module.py
tdata = threading.local()
log.info(tdata.userid)
Or is there any other way of doing it
Upvotes: 1
Views: 177
Reputation: 8346
Store it in the session (or alternatively, cookie) if it is per request.
As an alternative if you are not using sessions or cookies, have a look at Django's low level cache API. You can store the value in the cache and access it later.
>>> cache.set('userid', 234, 30)
Then in another context
>>> cache.get('userid')
234
Upvotes: 2