Rc Charly
Rc Charly

Reputation: 23

Odoo computed fields: result in unsupported operand type(s)

In Odoo 8 i have a computed field with a function which result in a error. Can't seem to make things work and need some help.

My test code:

from openerp import models, fields, api, _
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp

class plano(models.Model):
    _name = 'plano'
    _description = "variabelen plaat toeslagen, rillen kenmerken."

    name = fields.Char('Plano naam', required=True)
    constructie_id = fields.Char('Fefco constructie')
    testB_p1 = fields.Char('Breedte P1', help = 'Tekst veld P1 (variabele breedte P1)')
    toeslagB_p1 = fields.Float('toeslag breedte P1 (variabel Breedte P1)', digits=(3, 1))
    testL_p1 = fields.Char('Lengte P1', help = 'Tekst veld P1 (variabele lengte P1)')
    toeslagL_p1 = fields.Float('toeslag lengte P1 (variabel lengte P1)', digits=(3, 1))
    Kw = fields.Float('Kwaliteit dikte in mm', digits=(3, 0), help = "Wordt uit gerelateerd veld van model Quality gehaald.")


class calc(models.Model):

    @api.depends('name')
    def _functionb_p1(self):
    val1 = 0.0

        if plano.testB_p1 != 'H+':
            val1 = calc.hoogte + (2.0 * plano.Kw) + 2.0            
        elif plano.testB_p1 != 'B': 
            val1 = calc.breedte + (plano.toeslagB_p1 * plano.Kw)
    return val1

    _name = "calc"
    _description = "kostprijs berekening."


    name = fields.Many2one('plano', help = "Kostprijs berekening nummer e.g. C1234")
    lengte = fields.Float('Lengte in mm', digits=(4, 0), help = "Lengte in mm")
    breedte = fields.Float('Breedte in mm', digits=(4, 0))
    hoogte = fields.Float('Hoogte in mm', digits=(4, 0))
    aantal = fields.Float('Aantal stuks', digits=(4, 0))

    planob_p1 = fields.Float('Plano Breedte P1')
    planobt_p1 = fields.Float('Plano Breedte toeslag P1')   
    val1 = fields.Float(compute = '_functionb_p1', store=True,
                    string = 'Aanmaak Plano breedte P1',  
                    help = "Berekening vanuit functie _functionb_p1")

ERROR:

File "....8.0\test\models\calc.py", line 47, in _functionb_p1
val1 = calc.hoogte + (2.0 * plano.Kw) + 2.0
TypeError: unsupported operand type(s) for *: 'float' and 'Float'

Upvotes: 0

Views: 228

Answers (1)

CZoellner
CZoellner

Reputation: 14801

The TypeError is very weird. I never had such problems with Odoo Float...

But some hints to your computing function. It should use @api.multi or @api.one (second is deprecated) as additional decorator.

And then you should have a connection between plano and calc models. Your model relation (there is no one) doesn't allow the calculation you're looking for, because you need a plano instance/record and a calc instance/record.

You're computing your value on calc model, so i will try to get you the right method, with one condition: there is a Many2One field on calc model named plano_id.

@api.multi
@api.depends('name')
def _functionb_p1(self):
    for calc in self:
        val1 = 0.0
        plano = calc.plano_id
        if plano.testB_p1 != 'H+':
            val1 = calc.hoogte + (2.0 * plano.Kw) + 2.0
        elif calc.plano_id.testB_p1 != 'B':
            val1 = calc.breedte + (plano.toeslagB_p1 * plano.Kw)
        calc.val1 = val1

# my condition!
plano_id = fields.Many2One(comodel_name="plano", string="Plano")

Upvotes: 0

Related Questions