我有一个相关的字段设置,并且在开发人员模式下通过GUI将字段添加到FormView时工作正常。但是,当我将字段添加到自定义模块并尝试执行升级时,它会在x_work_order_num上的KeyError中断。我错过了什么?
其他一些说明:
my_sale.py
class my_sale_order(osv.osv):
_inherit = 'sale.order'
_columns = {
'x_work_order_num': fields.char('Work Order Number', size=64),
}
my_invoice.py
class my_account_invoice(osv.osv):
_inherit = 'account.invoice'
_name = 'account.invoice'
x_rel_order = fields.Many2one('sale.order', 'Related Sales Order')
x_ext_num = fields.Char(related='x_rel_order.x_work_order_num', string ="External Number")
@api.multi
def onchange_x_rel_order(self,order_id,context=None):
work_order_num = ""
if(order_id):
p = self.env['sale.order'].browse(order_id)
work_order_num = p.x_work_order_num
res = {
'value': {
'x_ext_num': work_order_num
}
}
return res
my_invoice.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="invoice_form_my" model="ir.ui.view">
<field name="model">account.invoice</field>
<field name="name">account.invoice.form</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="/form/sheet/group/group[2]/field[@name='account_id']" position="after">
<field name="x_rel_order" on_change="onchange_x_rel_order(x_rel_order)"/>
<field name="x_ext_num" readonly="1"/>
</xpath>
</field>
</record>
</data>
</openerp>
__的OpenERP __。PY
{
'name': 'My_Invoicing',
'version': '1.0',
'category':'Sales',
'description': 'My custom module',
'author': 'jeszy',
'depends':['base','sale','account','account_voucher'],
'data': ['my_invoice.xml'],
'demo': [],
'installable': True,
'auto_install': False,
}
Odoo错误:
Odoo Server Error
Traceback (most recent call last):
File "/opt/odoo/openerp/http.py", line 539, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/odoo/openerp/http.py", line 576, in dispatch
result = self._call_function(**self.params)
File "/opt/odoo/openerp/http.py", line 312, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/odoo/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/odoo/openerp/http.py", line 309, in checked_call
return self.endpoint(*a, **kw)
File "/opt/odoo/openerp/http.py", line 805, in __call__
return self.method(*args, **kw)
File "/opt/odoo/openerp/http.py", line 405, in response_wrap
response = f(*args, **kw)
File "/opt/odoo/addons/web/controllers/main.py", line 948, in call_button
action = self._call_kw(model, method, args, {})
File "/opt/odoo/addons/web/controllers/main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/addons/base/module/module.py", line 534, in button_immediate_upgrade
return self._button_immediate_function(cr, uid, ids, self.button_upgrade, context=context)
File "/opt/odoo/openerp/api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/addons/base/module/module.py", line 495, in _button_immediate_function
registry = openerp.modules.registry.RegistryManager.new(cr.dbname, update_module=True)
File "/opt/odoo/openerp/modules/registry.py", line 370, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/opt/odoo/openerp/modules/loading.py", line 351, in load_modules
force, status, report, loaded_modules, update_module)
File "/opt/odoo/openerp/modules/loading.py", line 255, in load_marked_modules
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
File "/opt/odoo/openerp/modules/loading.py", line 156, in load_module_graph
registry.setup_models(cr, partial=True)
File "/opt/odoo/openerp/modules/registry.py", line 194, in setup_models
model._setup_fields(cr, SUPERUSER_ID)
File "/opt/odoo/openerp/api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 372, in old_api
result = method(recs, *args, **kwargs)
File "/opt/odoo/openerp/models.py", line 3006, in _setup_fields
field.setup(self.env)
File "/opt/odoo/openerp/fields.py", line 468, in setup
self._setup_related(env)
File "/opt/odoo/openerp/fields.py", line 514, in _setup_related
field = recs._fields[name]
KeyError: 'x_work_order_num'
答案 0 :(得分:0)
不完全是某些人可能正在寻找的东西但在我的情况下,我所需要的是通过替换
的定义来解决的x_ext_num = fields.Char(string='External Number', compute='_compute_x_ext_num')
带
@api.multi
def onchange_x_rel_order(self, order_id, context=None):
ext_num = ''
if(order_id):
p = self.env['sale.order'].browse(order_id)
ext_num = p.x_work_order_num
res = {
'value': {
'x_ext_num': ext_num
}
}
return res
def _compute_x_ext_num(self):
for record in self:
p = self.env['sale.order'].browse(record.x_rel_order.id)
record.x_ext_num = p.x_work_order_num
我不需要相关选项,并将XML中的on_change绑定到方法:
{{1}}