An Easy Way to Modify Existing View [Odoo]

Odoo report and form modification

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.

Private Odoo Consultant. Reach me on linkedin.com/in/amirulm

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reviving a 2006 Macbook (Part 1)

What is State and Why Does it Matter?

Flying an autonomous drone using event driven state machine

Code for All Dispatch — August 2019 Edition

Bluehost vs GoDaddy Hosting: Which is Better?

Tips and Tricks on How to Starting Develop Automation Testing on Existing Projects

How to get better at programming.

Bacula in a virtual environment

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amirul M.

Amirul M.

Private Odoo Consultant. Reach me on linkedin.com/in/amirulm

More from Medium

Final Cut Pro vs Adobe Premiere for Beginners

How to learn Ukrainian

Grandfather’s Shop.

Thur | Mentally Tired | Long Day