adding-custom-fields-to-existing-views-odoo-v12.png
Blogger_636631377605715135.png
By: Niyas Raphy

How to Add Custom Fields to Existing Views in Odoo v12

In the pre-written blogs, I have discussed with you on varied yet connecting features such as how to create a new module, defining the views in the created module, adding the search view in the created module, etc. If you missed them or haven’t read about and is interested in knowing about, refer the following blog: How to Create a Module in Odoo 12 

In this blog,  we will be discussing how to add new fields to an existing model. To explain the blog, we will take the model sales order and see how one can add a custom field to the model and display the same in the sales order form view.

So either you can create a new module or you can try the same in your existing custom modules. So let’s get started. Understand that our intention here is to add a new field of type: character to the sale order.

Adding Custom field to the model:


adding-custom-fields-to-existing-views-odoo-v12

This is the sales order form we are holding presently. Now we will add a char field named additional_note below the field Confirmation date in the above form.

For that,  we need to know the name of the corresponding model to which we are adding the fields. For those who don't know about the model displayed in the view, they can go for it and easily be found out by activating the developer mode. After activating the developer mode, click on lady debugger button near the logged in user name in the top menu bar, and click on the Edit form view.

adding-custom-fields-to-existing-views-odoo-v12

Once you click there, you will get a pop up/wizard, in the pop up you can see the corresponding model name.

adding-custom-fields-to-existing-views-odoo-v12

So we get the model name ie, sale.order, so in our python file, we have to create a class and inherit this model.

# -*- coding: utf-8 -*-
from odoo import models, fields
class SaleOrder(models.Model):
_inherit = 'sale.order'
additional_note = fields.Char(string='Additional Note')

So if you look at the above code, you can see a class named SaleOrder is created. You can inherit the model named sale.order and add a new field.

adding-custom-fields-to-existing-views-odoo-v12

Now, we are done with the python side, do not forget to call the python file in the init file.

Adding the field to the view:

For adding the field to the view, we have to get the external id of the view to which we have to add the field. To get it, one has to do the same procedure that was done for getting the model name.

adding-custom-fields-to-existing-views-odoo-v12

So in the XML file, you can inherit this view and add the field we have created.

 <record id="view_order_form_inherit" model="ir.ui.view">
        <field name="name">Sale Order Custom</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <field name="confirmation_date" position="after">
                <field name="additional_note"/>
            </field>
        </field>
</record>

Above code will add the additional_note field to the sale order form view, you can give the id and name for the record as per the wish, keep naming of the record related to what is done in the record so that it will be easier to understand.

In the model and inherit_id field we have to specify the model as sale.order and in the inherit_id,  the external id of the form to which we are going to add the field has to be specified.

adding-custom-fields-to-existing-views-odoo-v12

After specifying the model and inherit_id, we have to specify the place where we have to add the new field. If you check the above image you can see that I have placed the new field name additional_note after the field named confirmation_date.

To get the name of the existing field, you can just place the mouse over the field.

So I just placed the mouse over the field named Confirmation date, to get the field name of it.

adding-custom-fields-to-existing-views-odoo-v12

<field name="arch" type="xml">
            <field name="confirmation_date" position="after">
              <field name="additional_note"/>
            </field>
</field>

Now, we are done with the XML, make sure that the corresponding XML file is called in the manifest file and upgrade the corresponding module.

adding-custom-fields-to-existing-views-odoo-v12

Once the custom code is upgraded or installed, one can see the newly added field in the sales order form.

So this is all about adding a custom field to an existing view. You can try one of yours and see how it works.




Comments

4

rached kaabi

31/07/2019 - 8:43AM

I'm new to python and tried to follow the instructions, where we can find the python file related to the view sale.order ? Thanks


Hi, in the above you said writing python code.... but in which python script file we need to write code?


Henry Ogbimi

25/07/2019 - 3:26PM

Good Day, How do I get the total score for a student in a module?


Mourad Meziou

12/05/2019 - 2:57AM

I'm new to python and tried to follow the instructions, where we can find the python file related to the view sale.order ? Thanks




Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message