with_context odoo v13 中的问题

with_context issue in oddo v13

我有一个函数 func1,它在上下文中有 DICTIONARY

def func1(self, num):
    num = self.env.get('career.lib').with_context({'start':True}).func2(num)
    return num

func1 中有一个 func2 的调用,它在上下文中有另一个字典。

def func2(self, num):
    if num.get('x', False):
        c = {'x': 123}
        a,b = self.env.get('x.help').with_context({'end':False,'middle':False}).func3(c)
        return c

with in func2 我有另一个函数 func3,我用了 self._contextfunc1func2 中获取所有上下文字典。

def func3(self, some_x):
    -------------
    --------------
    --------------
    --------------
    context = dict(self._context)
    ---------------
    ---------------
    
    return something

但上下文给了我——{'end':False,'middle':False}

但我需要 -- {'start':True,'end':False,'middle':False}

希望这对您有所帮助。谢谢

克里什

每次需要使用上下文传递数据时,您都在替换上下文。 您可以pass/update并通过这种方式获取上下文,

def func2(self, num):
    # Copy the Context
    ctx = self._context.copy()
    if num.get('x', False):
        c = {'x': 123}
        # Update the Context
        ctx.update({'end': False, 'middle': False})
        a, b = self.env.get('x.help').with_context(ctx).func3(c)
        return c

您正在用字典调用 with_context 时替换“原始”上下文。

您可以改用 kwargs 或命名参数:

self.env.get('x.help').with_context(end=False, middle=False)

my_context_update = {'end': False, 'middle': False}
self.env.get('x.help').with_context(**my_context_update)