如果要获取 当前 用户来编辑此模型,请使用threadlocals 。Threadlocals中间件将当前用户置于进程范围的变量中。拿这个中间件
from threading import local_thread_locals = local()def get_current_user(): return getattr(getattr(_thread_locals, 'user', None),'id',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)
查看有关如何使用中间件类的文档。然后,您可以在代码中的任何地方调用
user = threadlocals.get_current_user
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)