Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
3 Respostas
3738 Visualizações

I want to add a field to several models which is the date of the weekly accounting period (weekending_date).  Originally I added this field (and a function to set the value) in each model where I required the field.  This resulted in the calculation function being duplicated in several places.

I would like to create a single module that defines the field and the function to calculate it, which I can then inherit in each model where I need it.  That way I only have the function defined once and have consistency across all models.

However, can I do this without it creating a table with just that one field in it?  

When I inherit the module, I want the field to be added into the main model where I need to use the field.  For example

Model account_move should have weekending_date added to it.

Model sale_order should have weekending_date added to it.

Any suggestions for how to do this correctly?



Avatar
Cancelar
Autor

Thank you. I think this is correct. I have implemented my first AbstractModel.

Melhor resposta

I believe that is what AbstractModel is used for.

Avatar
Cancelar
Melhor resposta

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.

Avatar
Cancelar
Autor

Thank you very much Ashish. This was helpful. However on attempting to implement it, I was not able to use '_inherit' and 'fields' in the manifest file. That did not work and those properties are not listed in the official documentation for the syntax and properties in the manifest file.

Possibly that was from an earlier version of odoo?

In the end I implemented this as an AbstractModel, which contains the method to calculate the weekending_date. That model is then inherited by each of the models that require the weekending date.

Melhor resposta

Please check the following link for custom module:

https://youtu.be/Xya_fCNr6tw

Thanks & Regards

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
2
jul. 23
2912
1
mai. 23
2586
1
mar. 23
2896
1
abr. 25
2342
1
abr. 25
3035