Search code examples
pythonpython-3.xodoo

how to concatenate tow Values as strings into an odoo Char field?


i'm trying to get fields from another model then do some operation on them, there is no problem with logic but I'm getting this error when the methods runs

psycopg2.DataError: invalid input syntax for type double precision: "1.007 t"

these all what I have done

 class uom_custom(models.Model):
_inherit = 'product.template'

uom_qty = fields.Char(store=True,compute='get_qty')

@api.depends('qty_available')
def get_qty(self):
    uoms=self.env['uom.uom'].search(['&',('category_id', '=', self.uom_id.category_id.id),('show_qty','=',True)])
    if uoms.uom_type == 'bigger':
        self.uom_qty= str(str(self.qty_available / uoms.factor_inv) + ' ' + uoms.name)
    elif self.uom_type =='smaller':
        self.uom_qty= str(self.qty_available * uoms.factor_inv) + ' ' + uoms.name
    else:
        self.uom_qty= str(self.qty_available) + ' ' + uoms.name
    return self.uom_qty

so how can I display the value of mathematic operation and uom name beside it Thanks in advance


Solution

  • The error states that the column in database is defined as double precision. Are you sure you've restarted Odoo and updated your module?

    And there are some common mistakes in your compute method. Firstly and i can't repeat it often enough: try to stick to the Odoo naming guideline and name it compute_uom_qty. Secondly without a special decorator a compute method can and will be called with more than one record, so loop on that records. Thirdly: you search for uom.uom which can lead to more than one record, too. So either limit the search to one record or/and implement a check if something was found. uoms.name can lead to SingletonError. And at last: you don't have to return anything in compute methods.

    @api.depends('qty_available')
    def compute_uom_qty(self):
        for record in self:
            uoms = self.env['uom.uom'].search(
                [('category_id', '=', record.uom_id.category_id.id),
                ('show_qty','=',True)], limit=1)
            if uoms.uom_type == 'bigger':
                qty = record.qty_available / uoms.factor_inv
                record.uom_qty = "{} {}".format(qty, uoms.name)
            elif uoms.uom_type =='smaller':
                qty = record.qty_available * uoms.factor_inv
                record.uom_qty = "{} {}".format(qty, uoms.name)
            else:
                record.uom_qty = "{} {}".format(record.qty_available, uoms.name)