型号limit_choices_to = {'用户':用户}

型号limit_choices_to = {'用户':用户},第1张

型号limit_choices_to = {'用户':用户}

如果要获取 当前 用户来编辑此模型,请使用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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5666748.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存