3

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 ?

olive007
  • 760
  • 1
  • 12
  • 32

2 Answers2

1

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>
olive007
  • 760
  • 1
  • 12
  • 32
0

Regarding :How I access to a field during making demonstration data ?

In oodoo we use ref in xml and env.ref in python for referring the demo data id.

The syntax should be somthing like ref('module_name.xml_id')

Basically ref is an environment method returning the record matching a provided external id:

Here module_name is the name of your module(not model) and xml_id is the id of created demo demo data

for example;

 <field name="company_id" ref="base.main_company"/>

here we are accessing the demo data xml id main_company created in base module.

Hope this may help you understanding the demo data reference in odoo.

Prakash Kumar
  • 2,554
  • 2
  • 18
  • 28
  • Yes, I understand that, I try to set a value with a field of the object which I get with a external id. Not the object itself, but a field which is stored into it. – olive007 May 05 '16 at 08:47