Skip to Content
Menu
This question has been flagged
2 Replies
1741 Views

Greetings, I am familiar with the ability to create custom functions within a model in Odoo, however, I am inquiring if there is a user-friendly method for adding dynamic formulas directly from the form, such as inputting a short Python code that contains formula logic and executing it within the form. Has anyone had experience or knowledge of a solution for this within the Odoo platform? Any suggestions would be highly appreciated.

Avatar
Discard
Best Answer

It is possible to add dynamic formulas directly from the form in Odoo by using the "Computed Fields" feature. Computed Fields allow you to define a custom function that will be called when certain fields in the form are changed, and the result of that function will be stored in a specific field in the model.

You can create a computed field by going to the developer mode in Odoo and navigate to the desired model and fields. In the fields you can create a new field and select "computed" as the type. Then you will have to write a python function that will be called when the field is computed, this function can make use of other fields in the form to calculate the final value.

Alternatively, you can also use the "onchange" method on fields, this method is called when the value of the field is changed by the user, you can use this method to update other fields with the new value.

It's worth mentioning that both solutions require knowledge of python programming, and it's recommended to test the code before implementing it on the live system.

Avatar
Discard
Author

Hey
thanks a lot for the reply .. actually I already tried this . but I had a one2many field . And the calculation is dependent on other records and not the same record

Best Answer

In Odoo, it is possible to create dynamic formulas and calculations directly from the form using the compute attribute. The compute attribute allows you to specify a method that will be called when the field's value is needed. This method can contain any logic or calculations you need to perform, including the use of Python code.


The method should be decorated with @api.depends and should have the same name as the field that you want to calculate. The depends attribute should contain a list of fields that are used in the calculation.


Here is an example of how you might use the compute attribute to create a dynamic formula in an Odoo model:


python

Copy code

class MyModel(models.Model):

    _name = 'my.model'


    x = fields.Integer()

    y = fields.Integer()

    result = fields.Integer(compute='_compute_result')


    @api.depends('x', 'y')

    def _compute_result(self):

        for record in self:

            record.result = record.x + record.y

In this example, the result field is calculated by adding the values of the x and y fields, and the calculation is performed whenever one of those fields changes.


It's worth noting that you can also use compute attribute with the function fields, and you can use them in the form view and in the tree view, it also could be used in the search view.


Keep in mind that the method decorated with @api.depends should be on the same model that the fields it depends on, otherwise, it will not work as expected

Avatar
Discard
Related Posts Replies Views Activity
1
May 24
1494
1
May 24
1433
2
Jun 23
1447
1
Apr 24
23239
1
Nov 22
1400