Skip to Content
Menu
This question has been flagged
3 Replies
1127 Views

Hi I am running Odoo 15

This is my first foray in adding custom fields to Odoo while using developer mode. I have been playing around to customize the list that is displayed in the timesheets module. My end goal is to add an hourly rate field and turnover field which will be both computed fields and included in the list. 


I have created both these custom fields, x_hourly_rate and x_turnover, with the corresponding python code related to the computation shown below. Nothing is being displayed in shown in the list ( I have added these fields in the list). Code attached below. Any ideas? Thanks

(x_hourly_rate)

for record in self:
    hourly = 0 
    if record.employee_id == "Michael":
        hourly = 10
    elif record.employee_id == "Denis":
        hourly = 15
    else:
        hourly = 17
record.x_hourly_rate = hourly
Avatar
Discard
Author Best Answer

Hi Islam,


Your suggestion makes sense and I have tried it out but I still get zero on each line in the list. I suspect there is something else I am missing. For the x_turnover custom field I also tried the following basic code just to coax some output on the list but its not working also : (x_turnover has monetary type whilst unit_amount is float. Could this be the problem?)


for record in self:
    record.x_turnover = record.unit_amount




HI Midhun,

No effect. 


In case of x_turnover I have tried the following code just to test if anything is working but I still got 0 displayed for all the records in the list. (I am just multiplying the timesheet hours by a constant of 20)

for record in self:
    record.x_turnover = record.unit_amount * 20


Could it have something to do with the xml of the list? copied below
















Avatar
Discard
Best Answer

By doing following two changes you can achieve your desired output:
#1: I think "employee_id" is a Many2one field that return an object. So, you cannot compare with String value. Probably, you want to compare with employee name. Then, It would be record.employee_id.name == "Michael".

#2: Check Indentation of last line of your code.

Final code should like this:

for record in self:
    hourly = 0
    if record.employee_id.name == "Michael":
        hourly = 10
    elif record.employee_id.name == "Denis":         hourly = 15
    else:
        hourly = 17
    
    record.x_hourly_rate = hourly

You can try this.


 

Avatar
Discard
Best Answer

Hi, could be indentation of the last line. You can try with:

for record in self:
    hourly = 0 
    if record.employee_id == "Michael":
        hourly = 10
    elif record.employee_id == "Denis":
        hourly = 15
    else:
        hourly = 17
    
    record.x_hourly_rate = hourly

Avatar
Discard