Abstract models in Odoo 19 will make the development of modular and reusable applications much easier. For other models, abstract models can be considered as any kind of blueprint that enables developers to define common fields, methods, and behavior shared between several concrete models. With the use of abstract models, the developer will not only avoid repetitive code but also maintain consistency and provide much cleaner and more scalable designs.
Benefits of Using Abstract Models
- Code Reusability: By using abstract models, the developers can centralize the shared logic and avoid repetitive code. If several models share some similar functionality, you'll be able to define it once in the abstract model and use it everywhere.
- Consistency Across Modules: Since the same logic is inherited, behaviors remain consistent across all the models that make use of this abstract base. This presents no contradictions and makes the output predictable.
- Simplify Maintenance: For any changes in business rules or logic, it will need to be updated in only one place — that is, the abstract model. All the other inheriting models will automatically get their behavior updated.
- Enhance Scalability: Applications with abstracted models are easier to scale and extend. New models can inherit shared functionality easily, without cluttering the code base.
Now, let's say we have lots of different models - sale.order, employee.request, etc. - that all need approval functionality. Instead of defining the fields and methods related to approval in each model, we can define an abstract model that contains the logic once and then share it with all our models.
class ApprovalMixin(models.AbstractModel):
_name = 'approval.mixin'
_description = 'Common approval functionality for models'
approved_by = fields.Many2one('res.users', string='Approved By')
approved_date = fields.Datetime(string='Approved Date')
is_approved = fields.Boolean(string='Is Approved', default=False)
@api.model
def approve_record(self):
"""Method to approve a record."""
self.write({
'is_approved': True,
'approved_by': self.env.user.id,
'approved_date': fields.Datetime.now(),
})
Here, the ApprovalMixin is an abstract model providing reusable fields and a method to approve a record. This logic can now be inherited by multiple concrete models:
class SaleOrder(models.Model):
_inherit = ['sale.order', 'approval.mixin']
class EmployeeRequest(models.Model):
_name = 'employee.request'
_inherit = ['approval.mixin']
_description = 'Employee Request'
name = fields.Char(string='Request Title')
This setup allows both the Sale Order and Employee Request models to have approval functionalities without code duplication. If the logic changes in the future, you just update it on the abstract model, and the changes cascade automatically to all inheriting models.
Conclusion
Abstract models in Odoo 19 are a big step toward writing cleaner, modular, and future-friendly code. They let the developer design applications that can be maintained or adapted more easily to changing requirements. Instead of scattering similar code in multiple models, one can declare shared logic once and extend it where needed, maintaining uniformity and reducing the chances of inconsistencies. The abstract models bring order and clarity to major projects where a number of modules share functionality. They enable easier collaboration among teams, easier debugging, and faster feature development since it will be based on a solid, reusable foundation.
To read more about How Transient Models Work in Odoo 19, refer to our blog How Transient Models Work in Odoo 19.