An Easy Way to Modify Existing View [Odoo]

Odoo report and form modification

Amirul M.
2 min readMay 13, 2020
Photo by Tuân Nguyễn Minh on Unsplash

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.

Modify Form

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

class StockPickingInherit(models.Model):
_inherit = 'stock.picking'

send_by = fields.Char(
string="Dikirim Oleh"
)

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="name">stock.picking.inherit</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="arch" type="xml">
<field name="owner_id" position="after">
<field name="send_by"/>
</field>
</field>
</record>

These are the detailed explanation:

  1. I define an new id with the same pattern as new object’s name.
  2. Fill name with stock.picking.inherit.
  3. Create new field with name inherit_id that refer to id existing form (stock.view_picking_form).
  4. 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.

Note:

Position after is for placing new tag after field owner_id. If you rather to replace it, change the position value to replace.

Modify Report

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">
<t>
<center>
<img alt="Barcode" t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' % ('Code128', 'ABCD', 600, 150)"/>
<span>ABCD</span>
</center>
</t>
</td>
</xpath>
</template>

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.

Note:

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.

--

--