Search code examples
inheritanceodooevalodoo-11

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


I have a base module called theater with the following model called 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)

Then I create another extension module called theater_extensionand add an additional field to 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')

The problem comes when I add test data to theater_extension. I update the file __manifest__.py:

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

And I create the 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.

The problem is that it does not find the theater module in the following line:

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

Error reported:

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>

Solution

  • Actually the error is very simple but hard to spot, ref accepts a string representation of XML-ID and you forgot to put quotes around your IDs:

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

    Because you didn't put those quotes when the expression is evaluated python will treat them as variables and this is why you are having this error because there is no reference called theater.