Inheriting and Adding Field to One2many Field in Odoo

Abid Patel
29-Oct-2024 Updated : 29-Oct-2024

Learn how to inherit models and add fields to One2many fields in Odoo, enhancing your application's functionality and data management.

Inheriting and Adding Field to One2many Field in Odoo

Odoo provides powerful open-source ERP platform. It offers vast customization capabilities. When working with Odoo a common task arises. That is the need to modify existing models. This piece will delve into a common operation in Odoo. That is inheriting a model and adding a new field.

What is a One2many Field?

Odoo uses One2many field. It is handy to create one-to-many relationship. It relates two models. A single customer is a good example. It can have multiple sales orders. One2many field is the key to manage these related records.

Model Inheritance

For Odoo model inheritance there is a process. You may create a new model. It has to extend functionality of previous model. An example will clarify it better. Let's say we want to add a new field. This new field will be added to sale.order model. It will help in tracking the source of a sales order.

Let’s take a look at another example.

python

from odoo import models, fields

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    source = fields.Char(string="Source", help="The source of the sales order")

We inherit a model called sale.order in this example. We add a new field to the model. This field is called source. It stores information. What kind of information? Information about origin of the sales order.

We execute a code snippet to do this. It is an example of inheriting and adding field to a One2many field.

Field Addition to One2many Field

Picture having One2many field in the sale.order model. It links to sale.order.line. You desire to add field to sale.order.line. It shows whether an item is discounted.

Step 1: Inherit the sale.order.line Model

python

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    is_discounted = fields.Boolean(string="Is Discounted", default=False)

In this snippet we inherit the sale.order.line model. We add Boolean field. It is called is_discounted. The field will enable users to mark whether a certain line item has discount applied.

Step 2: Update the View

After adding the new field a step is needed. It is to update XML view. This action displays field in sale order form.

python

<record id="view_order_form_inherit" model="ir.ui.view">
    <field name="name">sale.order.form.inherit</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='order_line']" position="inside">
            <field name="is_discounted"/>
        </xpath>
    </field>
</record>

This XML snippet is significant. We inherit existing sale order form view. We add new field is_discounted. This is done inside order_line One2many field.

Conclusion

Customizing models and adding fields in Odoo allows for extensive personalization. This will accommodate specific business needs. The steps provided can be followed for effective field addition to One2many field. This enhancement boosts Odoo application functionality. You can now capture extra data. Relevant data for your operations.

Exploiting Odoo's sturdy ORM and view inheritance is a smart move. You can mold the platform to match your business processes. This action will result in improved efficiency. It also provides better data management.

Make a Comment

Your email address will not be published. Required fields are marked *