How To Add Fields In Form View In Odoo?
How To Add Fields In Form View In Odoo?
Your growing business needs a software which helps you in measuring business growth, improve performance and increase productivity. Odoo is one such software which contribute to the cause.
Odoo also provides an option to customize these software’s to design certain features of the app according to the user’s needs.
When we talk about Odoo Customization, it is not about development only. It's about diving deep into the business requirements, understand it and then give it a final shape. There is a misconception about the development cost and time. Custom odoo development is neither expensive nor time consuming. These two parameters wholly depend upon the business requirements.
In this blog we’ll be discussing one such simple customization in odoo. Let’s see how to add a new field to an existing view in odoo.
We’ll do this customization with the help of a custom module in odoo. If you want to learn, read our post on How to Create a Custom Module in Odoo.
In this case, we’ll learn how to add a new field to the Sale Order form view in Odoo.
This is what a typical Sale Order form view looks like, we’ll try to add the custom field to this form view.
So let us start, In the custom module, create a .py file inside the models folder.
In this file, we’ll create our custom field to be added to the form view. It is very important that the field that needs to be added to the form view is present inside the form view.
We’ll add this field using inheritance in odoo. In the .py file, we’ll inherit the model in which we are adding our custom field. In this case, we are adding a custom field to ‘sale.order’ model in odoo.
After adding the custom field we now need to add this field to the form view using view inheritance in odoo. So first of all we need to know the external id of the form view where we are adding our custom field.
We can check this external ID by activating the developer mode. After that, we’ll see a ‘bug’ icon in the menu. After clicking, a sub-menu will open showing various options.
Select the ‘Edit form view’ option.
A pop-up will open. In this pop-up, we can check the external id of the form view that we need to inherit.
Now our next step is to create a record id for this customization. The syntax for inheriting a form view is given below :
We can inherit any view using this syntax, just make sure to mention the correct module name followed by the external ID of the form view that needs to be inherited to the inherit id field.
Now we can add the field to the form view using xpath or we can say providing a path to the new custom field using an existing field of the form view.
The process for giving the xpath is very simple. We need to have a predefined field which will be used for giving this xpath. The syntax follows like this:
<xpath expr=//field[@name=’note’] position=”after/before/replace/inside”>
In this case we are using the ‘note’ field of the sales module for giving the path. This field must be present by default inside the form view in Odoo. We can specify the position of the field in the position tag. Make sure to close the <xpath> tag also.
This was the process for adding a custom field to the Sale order in odoo. But what if we want to add a field inside the order lines in odoo. We can do this easily like this.
Just add the custom field to the ‘sale.order.line’ model by inheriting it.
Now comes the tricky part, we cannot add the xpath for the order lines as we don’t have a separate view for that. So we’ll use the form view of Sale Order and provide xpath through it like this:
In this case, what we have done is we have given the xpath for the ‘product_id’ field of order lines through the tree view of order lines present inside a page in a notebook in odoo.
< xpath expr="//notebook/page/field[@name='order_line']
So it is very easy to add custom field inside a form view in odoo. You can add any field to any existing form view by inheriting that form view. If you have any question regarding the same, ask in the comment section. We are happy to help you.