Odoo 计算字段:导致不支持的操作数类型

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

在 Odoo 8 中,我有一个计算字段,其函数会导致错误。 似乎无法使事情正常进行,需要一些帮助。

我的测试代码:

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")

错误:

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'

TypeError 很奇怪。我从来没有遇到过 Odoo Float 这样的问题...

但对你的计算功能有一些提示。它应该使用 @api.multi 或 @api.one (第二个已弃用)作为附加装饰器。

然后你应该在 plano 和 calc 模型之间建立联系。你的模型关系(没有)不允许你正在寻找的计算,因为你需要一个 plano instance/record 和一个 calc instance/record.

您正在计算 calc 模型上的值,因此我会尝试为您提供正确的方法,但有一个条件:calc 模型上有一个名为 plano_id 的 Many2One 字段。

@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")