参考测试数据中的基础模块-Odoo 11

Refer to the base module in the test data - Odoo 11

我有一个名为 theater 的基本模块,其中包含以下名为 ticket 的模型:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ticket(models.Model):
    _name = 'theater.ticket'
    _rec_name = 'representation_id'

    price= fields.Float('Price',required=True)
    representation_id = fields.Many2one('theater.representation',string='Representation',required=True)

然后我创建另一个名为 theater_extension 的扩展模块,并向 ticket 添加一个附加字段:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ticket(models.Model):
    _inherit = 'theater.ticket'

    individualTicket_id = fields.Many2one('theater_extension.individualTicket',string='Individual Ticket')

当我将测试数据添加到 theater_extension 时,问题就来了。我更新文件 __manifest__.py:

'demo': ['demo/dataTest.xml'],

我创建了 dataTest.xml:

    <record id="ticket1" model="theater.ticket">
        <field name="representation_id" ref="theater.representation1"></field>
        <field name="price">22</field>
        <field name="seats_ids"
        eval="[(6, 0, [ref(theater.seat1),ref(theater.seat2)])]"></field>
    </record>

Note: The model seat and seat1 and seat2 are created in the base (theater) module.

问题是它没有找到以下行中的影院模块:

        eval="[(6, 0, [ref(theater.seat1),ref(theater.seat2)])]" model="theater.seat"></field>

报告错误:

odoo.tools.convert.ParseError: "<class 'NameError'>: "name 'theater' is not defined" while evaluating
'[(6, 0, [ref(theater.seat1),ref(theater.seat2)])]'" while parsing /home/usuario/odoo-dev/theater_extension/demo/dataTest.xml:18, near
<record id="ticket1" model="theater.ticket">
        <field name="representation_id" ref="theater.representation1"></field>
        <field name="price">22</field>
        <field name="seats_ids"
        eval="[(6, 0, [ref(theater.seat1),ref(theater.seat2)])]"></field>
</record>

实际上错误很简单但很难发现,ref 接受 XML-ID 的字符串表示,而您忘记在您的 ID 周围加上引号:

      eval="[(6, 0, [ref('theater.seat1'), .....])]"

因为您在计算表达式时没有放置这些引号 python 会将它们视为变量,这就是您出现此错误的原因,因为没有名为 theater 的引用。