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

Hi,


I'm using Odoo studio to add a computed float field to a product page.

From the inventory valuation report I have added a field on the list view that calculates the average value of each unit in stock, like this:


    for record in self:

      record['x_studio_field_kZ3CY'] = record.stock_value / record.qty_at_date


I've then added that same field to the General Information on the product variant page.


The problem is if the qty_on_hand is 0, then the calculation cannot run and so an error message is returned if I try to open the page of a product with a quantity of 0.


    ValueError: <class 'ZeroDivisionError'>: "float division by zero" while evaluating

    "for record in self:\n  record['x_studio_field_kZ3CY'] = record.stock_value / record.qty_at_date"


Obviously, if there's 0 stock, I just want that field to return a value of 0.


This seems like it should be a fairly simple thing to get around, by saying "if qty=0, skip the formula and return value of 0", but I've practically no experience with coding so don't really know where to start.


Any help would be appreciated.


Thanks.

Avatar
Discard
Author Best Answer

Hi Niyas,

I'm afraid that's not working :(

I also tried adding a '] I think you maybe missed?

for record in self:
if record['x_studio_field_kZ3CY'] !=0:
    record['x_studio_field_kZ3CY'] =  record.stock_value / record.qty_at_date

Either way, still no luck. Any other ideas?

Thanks.

Avatar
Discard
Author

Sorted it out.

for record in self:

if record['qty_at_date'] !=0:

record['x_studio_field_kZ3CY'] = record.stock_value / record.qty_at_date

Thanks Niyas

Best Answer

Hi,

You can easily add a if condition and do it,

for record in self:
if record['x_studio_field_kZ3CY'] !=0:
    record['x_studio_field_kZ3CY =  record.stock_value / record.qty_at_date


Thanks

Avatar
Discard
Related Posts Replies Views Activity
2
Jul 24
413
1
Jan 24
422
2
Apr 24
1951
1
Apr 23
3722
1
Aug 22
12616