Set default value while creating record from one2many field - odoo

一世执手 提交于 2019-12-01 01:57:43

I got the way how to do it.

In order to set default values directly while adding records in one2many fields, we need to set values in context with prefix default_field_name : value.

context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}"

Note: active_id is not available while you creating new record and with that new record if you provide value in one2many model then active id(s) won't be there. It's only accessible once you save the parent record.

Solution :

<field name="cash_forecast_ids" context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}">
    <tree string="Payment Scedule" editable="bottom">
        <field name="name"/>
        <field name="forecast_date" />
        <field name="foreign_currency_amount" required="1" />
        <field name="currency_id" domain="[('id','=',parent.currency_id)]" required="1" />
        <field name="purchase_order_id" invisible="1"/>
        <field name="company_id" domain="[('id','=',parent.company_id)]" required="1" /> 
    </tree>
</field>

If you want to add domain in fields of one2many model and in that you want to use the values of parent model then you can do it by the following way.

<field name="company_id" domain="[('id','=',parent.company_id)]" />

To send parent value to one2many field define context on one2many field in XML, to use context append "default_" as prefix to desired field e.g. "default_state" as a key and for value use parent table field name.

<field name="external_evaluation_ids" context="{'default_state':state}">
 <tree>
  <field name="state"/> <!--newly created field in one2many table-->
  <field name="child_table_field1"/>
  <field name="child_table_field2"/>
  <field name="child_table_field2"/>
 </tree>
</field>

In above code snippet we have one2many field and in that field we define context with key value. Here name of key will starts from "default_" and than the field name, the value should be the parent table field name which we want to display in one2many pop up form view. For further info http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!