Search Here

Get parent form value in one2many form view or pop up form view

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 analyze what you have read.
  1. What is context?
  2. Why we need context?
  3. How to use or define context in Odoo/OpenERP?

Problem Statement

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.

Solution

What is Context and How to Use It in Odoo

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. 

Read More: How to Create Model and Fields in Odoo


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. 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
  • Create same field with same type on one2many table/model as have already exists in parent table
  • Display newly created field in form view, at that time the field has no value
  • 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.


Code Description

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.

Read More: How to Display Multiple Form and Tree View for Same Model 

  • 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).


Post a Comment

11 Comments

  1. Thank you a lot, it's very useful to me

    ReplyDelete
  2. Replies
    1. class your_model_name(models.Model):
      _name='your.model.name'
      name=fields.Char(string='Name')

      Delete
  3. thanks, can you please help me to post complete Model structure file (.py) for both parent and child models to understand this? regards

    ReplyDelete
  4. what is plo_domain_ids.name ? -> its model or field on child or parent ? thanks

    ReplyDelete
  5. why office doc don't mention this ?

    ReplyDelete
  6. I face a problem when edit line. Context is none. Please help me.

    ReplyDelete