简体   繁体   中英

If a particular field output value will negative so that how can i set it to 0 when view the XML file in Odoo?

I am trying to calculate the difference value between the two fields in ORM. If the result value is negative it will the value 0 when I show this field value using XML.

excess_amount = fields.Float(string="Excess amount", compute='difference_excess_amount')
  
def difference_excess_amount(self):
    for rec in self:
        rec.excess_amount = rec.actual_total - rec.expected_total
<field name="excess_amount"/>

Please try below code

@api.depends('actual_total', 'expected_total')
def difference_excess_amount(self):
    for rec in self:
        excess_amount = rec.actual_total - rec.expected_total
        rec.excess_amount = 0 if excess_amount < 0 else excess_amount
@api.depends('actual_total', 'expected_total')
def difference_excess_amount(self):
    for rec in self:
        rec.excess_amount = rec.actual_total - rec.expected_total
        if rec.excess_amount < 0:
            rec.excess_amount = 0

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM