Use [] to find tag! Example: [flutter, javascript]

Odoo - Inheritance Model

Odoo - Inheritance Model

Artikel ini dibukukan pada buku Odoo ERP Development
access_time 29 January 2025 remove_red_eye 493 Kali spellcheck 389 Kata, 2897 Karakter
#odoo #python

Inheritance in Odoo is a mechanism that allows developers to extend or modify existing models without changing their core structure. Odoo provides three main types of inheritance:

  1. Classical Inheritance
  2. Extension Inheritance
  3. Delegation Inheritance

Let's discuss each one.

1. Classical Inheritance

Classical inheritance in Odoo is similar to the inheritance concept in object-oriented programming. In this method, a new model is created by inheriting an existing model, allowing reuse of its methods and attributes.

Example of Classical Inheritance

from odoo import models, fields

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

    partner_code = fields.Char(string='Partner Code')

In the example above, the res.partner model is inherited, and a new field partner_code is added without altering the original model definition.

2. Extension Inheritance

Extension inheritance is used when we want to add or modify fields and methods of an existing model without defining a new one. This is done using _inherit without declaring _name.

Example of Extension Inheritance

from odoo import models, fields

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

    is_verified = fields.Boolean(string='Verified')

    def verify_partner(self):
        for record in self:
            record.is_verified = True

Here, the is_verified field and verify_partner method are added to the res.partner model without changing its original structure.

3. Delegation Inheritance

Delegation inheritance is used when we want to create a relationship between two models without merging their fields or methods. This is done using a Many2one field to the inherited model.

Example of Delegation Inheritance

from odoo import models, fields

class CompanyBranch(models.Model):
    _name = 'company.branch'
    _inherits = {'res.partner': 'partner_id'}

    partner_id = fields.Many2one('res.partner', required=True, ondelete='cascade')
    branch_code = fields.Char(string='Branch Code')

In the example above, the company.branch model inherits res.partner through the partner_id field. This way, all fields and methods of res.partner can be accessed in company.branch, but their database tables remain separate.

When to Use Each Type of Inheritance?

  • Classical Inheritance: Used when creating a new model that fully inherits another model.
  • Extension Inheritance: Suitable for adding or modifying fields/methods in an existing model.
  • Delegation Inheritance: Useful when creating a relationship between models without merging their fields.

Conclusion

Inheritance in Odoo is a highly flexible feature that enables developers to extend and customize application functionality without modifying Odoo’s core code. By understanding the differences between these three types of inheritance, developers can optimize their application structure according to business needs.

Artikel ini dibukukan pada buku Odoo ERP Development
Navigasi Konten