继承时字段不存在报错account.invoice
Field does not exist error when inheriting account.invoice
使用 Odoo 10(取自 GitHub commit 7413b26
, branch 10.0
), installing a module which I'm porting over from Odoo 8 fails due to not finding a field in an inherited account.invoice
. Problem is, this field is created within the inherited model, and the problem persists down to the point of being able to create a MCVE 以说明行为:
invoice.py
:
from odoo import fields, models
class AccountInvoice(models.Model):
_inherit = 'account.invoice'
a = fields.Char()
invoice.xml
:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="account_invoice_form_view">
<field name="name">account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="type">form</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<field name="origin" position="after" >
<field name="a" />
</field>
</field>
</record>
</data>
</openerp>
__manifest__.py
是 {'name':'Invoice bug','depends':['account'],'data':['invoice.xml'],'installable':True}
而 __init__.py
就是通常的 import invoice
。
这样的代码失败了
ParseError: "Error while validating constraint
Field `a` does not exist
Error context:
View `account.invoice.form`
[view_id: 554, xml_id: invoice_bug.account_invoice_form_view, model: account.invoice, parent_id: 421]
None" while parsing /odoo/addons/invoice_bug/invoice.xml:4, near
[XML follows]
这里还有一些观察结果:
- 继承另一个模型和视图,例如
product.template
和视图 product.product_template_only_form_view
(使用 <field name="default_code" position="after">
,工作正常
- 使用
<xpath expr="//field[@name='origin']" position="after">
而不是 field position="after"
不会影响错误
- 其他模块,如原生
sale
模块,成功扩展account.invoice
和相同视图(account.invoice_form
)正确:sale/models/account_invoice.py:8, sale/views/sale_views.xml:653
- 一个similar bug exists since OpenERP 7
- 一个 Odoo 论坛成员看似 had the same problem 使用 Odoo 8 或 9(提供的示例导致相同的错误,前提是您将现在删除的
period_id
引用替换为 date
)
- 无论安装的模块或存储的记录如何,错误都会触发(在干净的数据库上测试 + 3 个其他安装)
是我的代码有误,还是有解决方法?
一切都很好,应该将字段添加到您的模型中。
对你的问题的一种解释是 odoo 没有到达你的代码。
您的项目中是否只有一个初始化文件,或者您正在使用多文件夹项目。检查你的缩进。
尝试更改标签
<openerp>..</openerp> to <odoo>..</odoo>
origin 字段在 account.invoice 和 account.invoice.line[= 中可用36=]型号。
如果您在表单视图中签入 invoice_form 那么我们将获得 2 倍的原始字段。
invoice_line_ids 内联树视图。
其他信息页面。
如果我们在之后写入字段位置,那么系统将在 invoice_line_ids 内联树视图中找到第一个字段,由于这个原因我们会出错。
但是 Odoo 给出了错误的错误信息。
你需要按照下面的xpath。
<xpath expr="//page[@name='other_info']/group/group[2]/field[@name='origin']" position="after">
<field name="a"/>
</xpath>
这可能对你有帮助。
使用 Odoo 10(取自 GitHub commit 7413b26
, branch 10.0
), installing a module which I'm porting over from Odoo 8 fails due to not finding a field in an inherited account.invoice
. Problem is, this field is created within the inherited model, and the problem persists down to the point of being able to create a MCVE 以说明行为:
invoice.py
:
from odoo import fields, models
class AccountInvoice(models.Model):
_inherit = 'account.invoice'
a = fields.Char()
invoice.xml
:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="account_invoice_form_view">
<field name="name">account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="type">form</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<field name="origin" position="after" >
<field name="a" />
</field>
</field>
</record>
</data>
</openerp>
__manifest__.py
是 {'name':'Invoice bug','depends':['account'],'data':['invoice.xml'],'installable':True}
而 __init__.py
就是通常的 import invoice
。
这样的代码失败了
ParseError: "Error while validating constraint
Field `a` does not exist
Error context:
View `account.invoice.form`
[view_id: 554, xml_id: invoice_bug.account_invoice_form_view, model: account.invoice, parent_id: 421]
None" while parsing /odoo/addons/invoice_bug/invoice.xml:4, near
[XML follows]
这里还有一些观察结果:
- 继承另一个模型和视图,例如
product.template
和视图product.product_template_only_form_view
(使用<field name="default_code" position="after">
,工作正常 - 使用
<xpath expr="//field[@name='origin']" position="after">
而不是field position="after"
不会影响错误 - 其他模块,如原生
sale
模块,成功扩展account.invoice
和相同视图(account.invoice_form
)正确:sale/models/account_invoice.py:8, sale/views/sale_views.xml:653 - 一个similar bug exists since OpenERP 7
- 一个 Odoo 论坛成员看似 had the same problem 使用 Odoo 8 或 9(提供的示例导致相同的错误,前提是您将现在删除的
period_id
引用替换为date
) - 无论安装的模块或存储的记录如何,错误都会触发(在干净的数据库上测试 + 3 个其他安装)
是我的代码有误,还是有解决方法?
一切都很好,应该将字段添加到您的模型中。 对你的问题的一种解释是 odoo 没有到达你的代码。
您的项目中是否只有一个初始化文件,或者您正在使用多文件夹项目。检查你的缩进。
尝试更改标签
<openerp>..</openerp> to <odoo>..</odoo>
origin 字段在 account.invoice 和 account.invoice.line[= 中可用36=]型号。
如果您在表单视图中签入 invoice_form 那么我们将获得 2 倍的原始字段。
invoice_line_ids 内联树视图。
其他信息页面。
如果我们在之后写入字段位置,那么系统将在 invoice_line_ids 内联树视图中找到第一个字段,由于这个原因我们会出错。
但是 Odoo 给出了错误的错误信息。
你需要按照下面的xpath。
<xpath expr="//page[@name='other_info']/group/group[2]/field[@name='origin']" position="after">
<field name="a"/>
</xpath>
这可能对你有帮助。