Enable Dark Mode!
how-to-add-a-field-to-an-existing-model-in-odoo-18.jpg
By: Safa KB

How to Add a Field to an Existing Model in Odoo 18

Technical

In any business application, models define the structure and behavior of data. Odoo’s modular architecture allows developers to easily extend existing models to accommodate new business requirements. One of the most common customization tasks is adding new fields to an existing model. Whether you need to store extra information on a customer record, sales order, or any custom model, Odoo makes this process straightforward and flexible.

Odoo 18 continues to support seamless model extensions through inheritance. This means you can add new fields to core models or custom ones without altering the base code. This approach ensures your customizations remain compatible with future updates and align with best development practices.

In this blog, we will walk through the process of adding a new field to an existing model in Odoo 18. By following these steps, you can capture and display additional information in both the backend and views with ease.

Step 1: Inherit the Model and Add the Field

To add a field, you need to inherit the existing model and define the new field in your custom module. Below is an example where we add a new customer_type field to the res.partner model:

from odoo import models, fields
class ResPartner(models.Model):
   _inherit = 'res.partner'
   customer_type = fields.Selection([
       ('regular', 'Regular'),
       ('vip', 'VIP'),
       ('supplier', 'Supplier')
   ], string='Customer Type')

In this example:

* We use _inherit to extend the res.partner model.

* We define a Selection field named customer_type.

* The string attribute sets the label that users will see on the form view.

You can use any field type, such as Char, Integer, Date, Many2one, and more, depending on your needs.

Step 2: Update the Form View to Display the Field

After adding the field to the model, you need to update the form view so users can see and interact with it. Use XML inheritance to inject your custom field into the appropriate view:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
   <record id="view_partner_form_inherit_customer_type" model="ir.ui.view">
       <field name="name">res.partner.form.inherit.customer.type</field>
       <field name="model">res.partner</field>
       <field name="inherit_id" ref="base.view_partner_form"/>
       <field name="arch" type="xml">
           <xpath expr="//field[@name='category_id']" position="after">
               <field name="customer_type"/>
           </xpath>
       </field>
   </record>
</odoo>

In this XML snippet:

* We inherit the existing res.partner form view.

* We locate the appropriate section using an xpath.

* We insert our new field inside the group tag of the form’s sheet, ensuring it appears alongside other fields.

Conclusion

Adding a new field to an existing model in Odoo 18 is a powerful way to customize your system to better suit your business needs. With just a few lines of code in both the model and view files, you can extend Odoo’s capabilities without touching the core logic.

This approach ensures your instance remains maintainable, scalable, and upgrade-friendly. Whether you’re capturing more customer information, logging internal references, or tracking custom attributes, extending models with custom fields helps you make the most of Odoo’s flexibility.

With this knowledge, you can now confidently tailor any Odoo model to meet your specific requirements, enhancing productivity and user experience.

To read more about How to Add Chatter to an Existing Model in Odoo 18, refer to our blog How to Add Chatter to an Existing Model in Odoo 18.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message