After reading the article, answer of these questions help you to reflect upon and analyze what you have read.
- What is context?
- Why we need context?
- 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.
Read More: How to Create Form and Tree View in Odoo
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).
11 Comments
Thank you a lot, it's very useful to me
ReplyDeletewelcome dear
DeleteI want to see model class.
ReplyDeleteclass your_model_name(models.Model):
Delete_name='your.model.name'
name=fields.Char(string='Name')
thanks, can you please help me to post complete Model structure file (.py) for both parent and child models to understand this? regards
ReplyDeletewhat is plo_domain_ids.name ? -> its model or field on child or parent ? thanks
ReplyDeleteits one2many field
Deletewhy office doc don't mention this ?
ReplyDeletethere is lack of documentation... that's why
DeleteI face a problem when edit line. Context is none. Please help me.
ReplyDeletecan you please share your code
Delete