Accessing request.user in models.py and signals.py
Use a custom middleware class
from stackoverflow
from threading import local
_thread_locals = local()
def get_current_user():
return getattr(_thread_locals, 'user', None)
class ThreadLocals( object ):
"""Middleware that gets various objects from the request object and saves them in thread local storage."""
def process_request( self, request ):
_thread_locals.user = getattr( request, 'user', None )
// in settings.py
MIDDLEWARE_CLASSES += (
"your.path.to.middleware.ThreadLocals"
)
It is important that you use threading.local
instead of using a dictionary with threading.current_thread
, as the latter may lead to a memory leak (old values remaining in the current_thread as long as the application is running)
A library is also available for this purpose.
Author And Source
이 문제에 관하여(Accessing request.user in models.py and signals.py), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@sjp5554/Accessing-request.user-in-models.py-and-signals.py저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)