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:
- Add new fields to the model.
- Override or extend existing methods.
- 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:
_inheritspecifies the model you want to extend (res.partnerin 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) andx_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_idspecifies the ID of the view you want to inherit (base.view_partner_form).xpathis used to locate the position in the existing view where the new fields will be added.- The new fields (
x_national_idandx_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 (basein this case).data: Lists the XML files to be loaded.
4. Test the Changes
- Restart the Odoo server:
./odoo-bin -u custom_module_name - Navigate to the form view of the
res.partnermodel (e.g., Contacts). - Verify that the new fields (
National IDandIs Verified) appear as expected.
Best Practices
- Use
x_Prefix:- Always prefix custom fields with
x_to avoid naming conflicts.
- Always prefix custom fields with
- Comment Your Code:
- Add comments to explain the purpose of your customizations.
- Follow Naming Conventions:
- Use descriptive names for fields and XML records.
- Test Thoroughly:
- Test your customizations in a development environment before deploying to production.
- 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!