Search code examples
xmlinheritanceodooopenerp-7openerp-8

Odoo - access to a field from xml when demo creation


I am developing a module in Odoo. I overload by delegation the class "project.task" form Project module of Odoo.

py file

class Intervention(models.Model):

     _name = "module.intervention"
     _inherits = {
         "project.task": "task_id"
     }

     ### Fields
     task_id = fields.Many2one(
         "project.task",
         ondelete="cascade",
         required=True
     )

     ### Overlord
     @api.model
     def create(self, vals)
         # do lot of thing
         intervention = super(Intervention, self).create(vals)
         # link the object with 0ne2one relation
         intervention.task_id.intervention_id = intervention

xml file

<record id="module.intervention0" model="module.intervention">
    <field name="type_id" ref="module.interventionTypeDirect"/>
    <field name="project_id" ref="module.project0"/>
    <field name="user_id" ref="module.user0"/>
</record>

<record id="module.activity0" model="accoanunt.alytic.line">
    <field name="unit_amount">2.5</field>
    <field name="task_id" eval="ref('module.intervention0').task_id"/>
    <field name="account_id" ref="module.project0"/>
    <field name="user_id" ref="module.user0"/>
</record>

I got below error when I try to access to a field

"ParseError: 'int' object has no attribute 'task_id'"

I also tried to use 'env' in eval.

<field name="task_id" eval="env['module.intervention'].browse([ref('module.intervention0')], limit=1).task_id"/>

ParseError: "name 'env' is not defined" while parsing demo.xml

My question is:

How I access to the fields of a object into eval expression during xml parsing ?


Solution

  • To solve my problem I use a false model which he create a external id for the analytics account.

    class AffairUpdateAnalyticAccount(models.Model):
    
        _name = "module.affair_update_analytic_account"
    
        @api.model
        def create(self, vals):
    
            # Generate an external ID
            externalIds = self.env["ir.model.data"].search([('model', '=', 'module.affair')])
            for externalId in externalIds:
                if self.env["ir.model.data"].search_count([("name", "=", "%s_analytic_account" % externalId.name)]) == 0:
                    test = self.env["ir.model.data"].create({
                        'name' : "%s_analytic_account" % externalId.name,
                        'res_id': self.env["module.affair"].browse([externalId.res_id]).project_id.analytic_account_id.id,
                        'model': "analytic.account",
                        'module' : 'module'
                    })
    
            return super(AffairUpdateAnalyticAccount, self).create(vals)
    
    <!-- Affair record -->
    
    <record id="module.affairUpdateAnalyticAccount" model="module.affair_update_analytic_account">
        <field name="id">1</field>
    </record>
    
    <record id="module.activity0" model="account.analytic.line">
      <field name="unit_amount" eval="2"/>
      <field name="task_id" ref="module.intervention0_project_task"/>
      <field name="account_id" ref="module.affair0_analytic_account"/>
      <field name="user_id" ref="module.user0"/>
      <field name="is_timesheet">True</field>
    </record>