Answer with Step-by-Step Guide:
Step 1: Understand the Purpose of _inherit and _name
- _name defines a new model.
- - _inherit is used to extend or override an existing model If both are used incorrectly together, you might unintentionally create a new model instead of overriding an existing one.
Python
# models/product_extension.py
from odoo import models, fields
class ProductTemplate(models.Model):
_name = 'product.template' # ❌ This will redefine a new model, not extend!
_inherit = 'product.template' # ❌ Using _name and _inherit same = bad override
custom_field = fields.Char(string="Custom Field")
Problem: This creates a new conflicting model instead of extending the existing product.template.
Step 2: Use Only _inherit to Extend Existing Model
Python
# models/product_extension.py
from odoo import models, fields
class ProductTemplate(models.Model):
_inherit = 'product.template' # ✅ Correct way to extend existing model
custom_field = fields.Char(string="Custom Field")
Result: This adds custom_field to the product.template model.
Step 3: Use _name Only When Creating a New Model
Python
# models/my_custom_model.py
from odoo import models, fields
class MyCustomModel(models.Model):
_name = 'my.custom.model' # ✅ New model name
name = fields.Char(string="Name")
active = fields.Boolean(default=True)
Tip: Avoid using the same _name as any core model unless you're sure you're replacing it.
Step 4: Check Model Registration
After defining your model, ensure it is registered in init.py and added to manifest.py
Python
# __init__.py
from . import models
# models/__init__.py
from . import product_extension
# __manifest__.py
'depends': ['base', 'product'], # product dependency is important if extending product.template
Step 5: Upgrade the Module Properly
Run the following to upgrade and reflect the changes:
Bash
odoo -u your_module_name -d your_database
Or from UI:
Go to Apps → Update App List → Upgrade your module.
When changes made to an overridden Odoo model do not reflect in the system, the issue typically stems from improper model inheritance, misconfigured field references, or missing XML updates in the view structure. Odoo customization involves extending the core ERP framework by modifying models, views, and workflows, often through custom modules and Python code. Common pitfalls in this process include using incorrect XPath expressions in inherited views, failing to trigger module updates after changes, or conflicts in model inheritance that prevent proper loading. Ensuring accurate overrides requires a clear understanding of Odoo's ORM, view inheritance mechanism, and database updates through module reloads and cache clearing.
Top comments (0)