How to Inherit an Existing Model and Add New Fields in Odoo

How to Inherit an Existing Model and Add New Fields in Odoo

Inheriting existing models is a common practice in Odoo development, allowing you to extend or customize the functionality of core modules without modifying the original code. This approach ensures compatibility with future updates and maintains the integrity of the system. In this post, we’ll walk you through the process of inheriting an existing model and adding new fields, covering both the Python and XML aspects.


What is Model Inheritance in Odoo?

Model inheritance in Odoo is a way to extend an existing model’s structure or behavior. By inheriting a model, you can:

  1. Add new fields to the model.
  2. Override or extend existing methods.
  3. Modify the model’s views or workflows.

Odoo provides two types of inheritance:

  • Classical Inheritance: Extending the functionality of a model.
  • Prototype Inheritance: Changing the behavior of the model.

In this guide, we’ll focus on adding new fields using classical inheritance.


Step-by-Step Guide to Adding New Fields

1. Define the Python Code

To inherit a model and add new fields, you need to create a Python file in your custom module.

Example: Inheriting the res.partner Model

from odoo import models, fields

class ResPartnerInherit(models.Model):
    _inherit = 'res.partner'

    x_national_id = fields.Char(string='National ID')
    x_is_verified = fields.Boolean(string='Is Verified')

Explanation:

  • _inherit specifies the model you want to extend (res.partner in this case).
  • x_ prefix is used for custom fields to avoid conflicts with existing fields.
  • Two new fields are added: x_national_id (a Char field) and x_is_verified (a Boolean field).

2. Create the XML View

To make the new fields visible in the user interface, you need to modify the views associated with the inherited model.

Example: Adding Fields to the Form View of res.partner

Create an XML file in your module’s views directory (e.g., views/res_partner_views.xml):

<odoo>
    <record id="view_res_partner_form_inherit" model="ir.ui.view">
        <field name="name">res.partner.form.inherit</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="arch" type="xml">
            <xpath expr="//sheet/group" position="inside">
                <group string="Custom Fields">
                    <field name="x_national_id"/>
                    <field name="x_is_verified"/>
                </group>
            </xpath>
        </field>
    </record>
</odoo>

Explanation:

  • inherit_id specifies the ID of the view you want to inherit (base.view_partner_form).
  • xpath is used to locate the position in the existing view where the new fields will be added.
  • The new fields (x_national_id and x_is_verified) are grouped for better organization.

3. Update the Manifest File

Include the new XML file in the __manifest__.py file of your module to ensure it loads correctly.

{
    'name': 'Custom Partner Fields',
    'version': '1.0',
    'category': 'Custom',
    'depends': ['base'],
    'data': [
        'views/res_partner_views.xml',
    ],
    'installable': True,
    'application': False,
}

Explanation:

  • depends: Specifies the modules your custom module relies on (base in this case).
  • data: Lists the XML files to be loaded.

4. Test the Changes

  1. Restart the Odoo server: ./odoo-bin -u custom_module_name
  2. Navigate to the form view of the res.partner model (e.g., Contacts).
  3. Verify that the new fields (National ID and Is Verified) appear as expected.

Best Practices

  1. Use x_ Prefix:
    • Always prefix custom fields with x_ to avoid naming conflicts.
  2. Comment Your Code:
    • Add comments to explain the purpose of your customizations.
  3. Follow Naming Conventions:
    • Use descriptive names for fields and XML records.
  4. Test Thoroughly:
    • Test your customizations in a development environment before deploying to production.
  5. Keep Backups:
    • Always back up your database and module files before making changes.

Conclusion

Inheriting existing models and adding new fields in Odoo is a straightforward yet powerful way to customize the system. By following the steps outlined in this guide, you can extend the functionality of any model while maintaining compatibility with future updates. With practice, you’ll master Odoo development and unlock its full potential for your projects.

Let us know in the comments if you found this guide helpful, and feel free to share your questions or experiences!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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