Problem Statement

Recommend:

Solution

<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>

Code Description

To get parent value in one2many we may use context in XML. First of all I will define you the problem statement that why we need this scenario. And using context in xml what we could achieve.After reading the article, answer of these questions help you to reflect upon and analyse what you have read.1- What is context?2- Why we need context?3- How to use or define context in Odoo/OpenERP?For example I have a form and in this form view I have one2many field represented as a tree view in my parent form view. When a user clicks on "Ad an Item" a pop up form will be opened. In that form I would like to display some parent form view field. Or I want to perform some attribute action based on parent form field data. Here attribute action mean I want to visible/invisible or read only some fields of one2many field depends on parent field value.To achieve above scenario we will use context (Context is a dictionary used in python to pass data from one model to another model ) in one2many field in XML. The purpose of using context in one2many field is that by using this we get parent view field and display that field in our one2many form or tree view. Once we got parent form view fields in our one2many or child view than its not difficult to use this field in attributes to handle visibility and read only issue.In above screen shot we have a relational field (one2many) in a parent form view. In parent view we have a field named state, and we need this field value in one2many form view as shown below.Follow these steps to add context on one2many field.1- Create same field with same type on one2many table/model as have already exists in parent table2- Display newly created field in form view, at that time the field has no value3- 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.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.4- Now we have same field as we have in our parent model and the value comes to that field from parent model (by using context).