To add a field to multiple models and define a calculation function for the field in a single module, you can create a new module and use the _inherit attribute in the module's manifest file to inherit the models that you want to add the field to.
In the new module, you can define the field and the calculation function for the field in the fields.py file. For example, you can create a fields.py file with the following content:
from odoo import fields, models
class WeekendingDate(fields.Date):
def _compute_weekending_date(self):
# Code to calculate the weekending date
pass
In this code, the WeekendingDate class is a subclass of the fields.Date class, and it defines a _compute_weekending_date method that can be used to calculate the weekending date.
Next, in the module's manifest file, you can use the _inherit attribute to inherit the models that you want to add the weekending date field to, and add the weekending date field to the models using the fields.Date class. For example, you can add the following lines to the manifest file:
'_inherit': ['account.move', 'sale.order'],
'fields': {
'weekending_date': fields.Date('Weekending Date', compute='_compute_weekending_date'),
}
In this code, the _inherit attribute is used to inherit the account.move and sale.order models, and the fields attribute is used to add the weekending_date field to the models. The weekending_date field is defined using the fields.Date class, and it has a compute attribute that specifies the _compute_weekending_date method as the calculation function for the field.
By using the _inherit attribute and the fields attribute in this way, you can add the weekending date field to multiple models in a single module, and define a calculation function for the field that will be used consistently across all the models. This will allow you to avoid duplicating the calculation function in multiple places, and ensure consistency across all the models that have the weekending date field.
Thank you. I think this is correct. I have implemented my first AbstractModel.