UnboundLocalError:在赋值之前引用的局部变量'context' - Odoov8到Odoov10社区

时间:2017-03-17 20:11:20

标签: python openerp odoo-8 odoo-10

我有这种方法,我已经从v8迁移到v10社区:

@api.model
def create(self, vals):
    """ To create a new record,
    adds a Boolean field to true
    indicates that the partner is a company
    """
    if context is None:
        context = {}
    context.update({'create_company': True})
    return super(ResUsers, self).create(vals) 

@api.multi
def write(self, values): 
    """ To write a new record,
    adds a Boolean field to true
    indicates that the partner is a company
    """
    context = dict(context or {})
    context.update({'create_company': True})
    return super(ResUsers, self).write(values) 

当我点击这些布尔值时,它会抛出这个:

Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 862, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 854, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 681, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 672, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/model/res_users.py", line 52, in write
context = dict(context or {})
UnboundLocalError: local variable 'context' referenced before assignment

我已将context = context {}更改为context = dict(context or {}),但我不确定是否需要它。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

只有您可以通过self._context或self.env.context访问上下文。但在这里我认为你错误地定义了

答案 1 :(得分:1)

你可以写创作&使用以下语法编写方法。

@api.model
def create(self, vals):
    """ To create a new record,
    adds a Boolean field to true
    indicates that the partner is a company
    """
    context=dict(self._context or {})
    context.update({'create_company': True})
    return super(ResUsers, self.with_context(context)).create(vals)

@api.multi
def write(self, values): 
    """ To write a new record,
    adds a Boolean field to true
    indicates that the partner is a company
    """
    context=dict(self._context or {})
    context.update({'create_company': True})
    return super(ResUsers, self.with_context(context)).write(vals)
  
    

使用with_context可以将上下文传递给超级方法

         

使用self._context,您将获得上下文,默认情况下,上下文为frozendict。你必须转换为dict,否则你会得到错误。

         

当你使用with_context调用super方法时,那时你必须传递现有的上下文以及其他明智的超级方法将不会获得所有子方法上下文。