Let's take an example of Sale Order Line. Scenario is like when we add Product in sale order line at time, the Product Category name should be fill up. For that we need to do two things.
- add related field in 'sale.order.line' and give it in view at proper place
- need to override onchange of product, if we don't want to override the onchange than at the time of record save it will also save the product category name.
Here is .py code:
Old API:
class sale_order_line(osv.Model):
_inherit = 'sale.order.line'
_columns = {
'product_categ_name': fields.related('product_id', 'categ_id', 'name', type='char', string='Product Category', store=True, readonly=True),
}
New API:
from openerp import api, fields, models, _
class SaleOrderLine(models.Model)
_inherit = 'sale.order.line'
product_categ_name = fields.Char(related='product_id.categ_id.name', string='Product Category', store=True, readonly=True)
Here is .xml file
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_product_category_related_sale_line" model="ir.ui.view">
<field name="name">view.product.category.related.sale.line</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/form/group/group/field[@name='product_id']" position="after">
<field name="product_categ_name"/>
</xpath>
</field>
</record>
</data>
</openerp>
Here is screen shot of it
More about on related field, take a look here
I hope you like this article. Share your views to improve content. Happy Learning !!!
Youtubde Video