1

I have a parent model (Plant) which sends the active_id (plant_id) in the context as a default:

class Plant(models.Model):
    _name = 'db.plant'
    _description = 'db.plant'
    name = fields.Char("Name", required=True)
    ...

Plant view with a many2one relation between them:

<odoo>
  <data>

    <record id="plant_list_view" model="ir.ui.view">
        <field name="name">db.plant.view</field>
        <field name="model">db.plant</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                ...
            </tree>
        </field>
    </record>

    <record id="plant_form_view" model="ir.ui.view">
        <field name="name">db.form.view</field>
        <field name="model">db.plant</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group string="Plant">
                        <field name="name"/>
                        ...
                    </group>
                    <group string="Components">
                        <notebook>
                            <page string="Data Sources">
                                <field name="data_source_ids" context="{'default_plant_id': active_id}">
                                    <tree >
                                        <field name="name"/>
                                        ...
                                        <!-- <field name="plant_id"/> -->
                                    </tree>
                                </field>
                            </page>
                        </notebook>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="plant_action" model="ir.actions.act_window">
        <field name="name">plant</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">db.plant</field>
        <field name="view_mode">tree,form</field>
    </record>

</data>

and his child model (Data_source):

class DataSource(models.Model):
    _name = 'db.data_source'
    _description = 'db.data_source'

    name = fields.Char("Name", required=True)
    plant_id = fields.Many2one('db.plant', name="Plant", required=True)

data_source view:

<odoo>
  <data>
    <record id="data_source_list_view" model="ir.ui.view">
        <field name="name">db.data_source.view</field>
        <field name="model">db.data_source</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                ...
            </tree>
        </field>
    </record>

    <record id="data_source_form_view" model="ir.ui.view">
        <field name="name">db.form.view</field>
        <field name="model">db.data_source</field>
        <field name="arch" type="xml">
            <form>
                <group>
                    <field name="name"/>
                    ...
                </group>
                <group>
                    <field name="plant_id" domain="[('id', '=', context.default_plant_id)]" />
                </group>
                <group>
                </group>
            </form>
        </field>
    </record>

    <record id="data_source_action" model="ir.actions.act_window">
        <field name="name">Data Source</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">db.data_source</field>
        <field name="view_mode">tree,form</field>
    </record>
</data>

I know that the default_plant_id is received in the child because the plaint_id domain is filtering just my plant_id but I am not able to set it automatically as a default value. I need to click on the list and select my plant_id (which is the only element in the list).

I also tried adding an @api.on_change("plant_id") in the child (data_source) in two ways:

Option 1:

    @api.onchange('plant_id')
    def onchange_plant_id(self):
    if self.plant_id:
        context = dict(self._context or {})
        return [(0,0,{'plant_id': context.get('default_plant_id')})] 

giving me this exception:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 640, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 316, in _handle_exception
    raise exception.with_traceback(None) from new_cause
AttributeError: 'list' object has no attribute 'get'

Option 2 (I am not sure what to send as a response, but the write statement is also not working):

    @api.onchange('plant_id')
    def onchange_your_many_to_one_field(self):
      context = dict(self._context or {})
      self.plant_id.write({'id':context.get('default_plant_id')})})
      res = {}
      return res

I need to auto-select my plant as an option in the data_source view before saving it.

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51

1 Answers1

0

There is no need of onchange method or anything on xml side - context level. You can simply use field plant_id in a relation one2many field data_source_ids.

Try with this:

data_source_ids = fields.One2many("db.data_source", "plant_id")
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58