An Easy Way to Modify Existing View [Odoo]
Odoo report and form modification
In a rapid development, I often use the existing template/framework and then modify/create new only if the existing feature doesn’t meet the requirement. This way can be applied to Odoo too. But how? Let’s take an example to modify a form and a report.
I want to add new field in existing form of Transfer. First, I write this code in existing model (or can be in new model).
_inherit = 'stock.picking'
send_by = fields.Char(
Create new object that inherit stock.picking then define new field (i.e. send_by) .
For XML view, same with model, I write in existing view (or can be in new view).
<record id="stock_picking_inherit" model="ir.ui.view">
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="arch" type="xml">
<field name="owner_id" position="after">
These are the detailed explanation:
- I define an new id with the same pattern as new object’s name.
- Fill name with stock.picking.inherit.
- Create new field with name inherit_id that refer to id existing form (stock.view_picking_form).
- Inside field name arch, I write an XML structure that I want to add. In this case, I write field name=”owner_id” position=”after” for targeting specific tag field with owner_id name and field with send_by name inside.
Position after is for placing new tag after field owner_id. If you rather to replace it, change the position value to replace.
For modifying PDF report, I create new view file inside report folder.
<template id="report_label_inherit" inherit_id="product.report_simple_label">
<xpath expr="//td[@class='text-center align-middle']" position="replace">
<td class="text-center align-middle" id="test_inherit">
<img alt="Barcode" t-att-src="'/report/barcode/?type=%s&value=%s&width=%s&height=%s' % ('Code128', 'ABCD', 600, 150)"/>
I define new template id with inherit_id product.report_simple_label. Inside it, locate the element with xpath tag. In this case, I replace the existing with image barcode.
If you create new files, be sure to import them in init file for new models and/or define them in manifest file for new views.