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

I am new to Odoo :)

I would to understand how can I use the Compute field.

For example I added a new model x_fleet and on it a new integer field x_value. But using the bellow python Compute script code it's not working (the value of this field is always 0):

Related Field:

Dependencies: x_value

Compute:

for record in self:
  record['x_value'] = 100


I selected the Stored and Indexed flag, also Tracking: "Always".

What I am doing wrong?


This example is my test to understand how this Compute is working.

My final aim is to use the Compute feature in order to interact between models(tables), for example when a new entry of this model is created and the field "km" is filled (ie. x_service.trip.km), in automatic to add the value of x_service.trip.km to total km: fleet.vehicle.odometer.value



Avatar
Discard
Author Best Answer

Do you use this code inside the Compute field or it's something that you modify directly on .py file?


Spasiba, I will try and let you know!

Avatar
Discard

do not quite understand. This is the code from my module that automatically considers the initial from the surname. The compute field and the function that is used in this field. Just your mistake is likely that it was not in front of the function, the decorator depends or onchange. And this code is in the py file of your module

Best Answer

Hi, my code work use api depends or onechange!

 firstname = fields.Char(string="Name", required=True)
 initials_first = fields.Char(compute='_compute_first',string="name initials first")
 @api.depends('firstname')
 def _compute_first (self):
  for record in self:
      if type (record.firstname) != bool:
        if len(record.firstname) >1:
            record.initials_first = u"\u00A0"+ record.firstname [1]+"."
        else:
            record.full_name = "empty"
      else:
           record.full_name = "empty"

Avatar
Discard