Python UnboundLocalError:局部变量';上下文';分配前参考-奥多夫8至奥多夫10社区

Python UnboundLocalError:局部变量';上下文';分配前参考-奥多夫8至奥多夫10社区,python,openerp,odoo-8,odoo-10,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_compa

我有一个从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或{})
,尽管我不确定是否需要它


有什么想法吗?

只有您自己才能访问上下文。\u context或self.env.context。但是在这里,我认为您的定义是错误的

只有您自己才能访问上下文。_context或self.env.context。但在这里,我认为您的定义是错误的

您可以使用以下语法编写create&write方法

@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可以将上下文传递给super方法

使用self.\u context您将获得context,默认情况下context是frozendict。你必须用字典转换,否则你会出错的

当您使用with_上下文调用超级方法时,此时您必须传递现有上下文,而其他wise超级方法将不会获得所有子方法上下文


您可以使用以下语法编写create&write方法

@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可以将上下文传递给super方法

使用self.\u context您将获得context,默认情况下context是frozendict。你必须用字典转换,否则你会出错的

当您使用with_上下文调用超级方法时,此时您必须传递现有上下文,而其他wise超级方法将不会获得所有子方法上下文