不计算的计算字段和不起作用的默认字段(odoo-12)

时间:2019-04-04 20:05:09

标签: odoo-12

我在模型中的此字段中带有一种方法,旨在检测many2one中的更改:

manytoone = fields.Many2one(comodel_name="product.template", string="Many to One",
                                required=False, )

cambio_realizado = fields.Boolean(string="Cambios en Many to One", default=False)

@api.onchange('manytoone')
def _detectar_cambio(self):
    self.cambio_realizado = True

否有效,布尔值字段中的default = False。始终从True开始。

另一个问题是计算字段无效:

@api.multi
def computer_function(self):
    self.computation = 243+234

    computation = fields.Float(string="Result: ", compute=computer_function, readonly=True)

结果为零...始终在视图中。

可以确定,并且模型可以继承

我的班级和我的观点与提示:

class CustomModule(models.Model):
    _inherit = 'sale.order'

    name = fields.Char('campos relacionales, decoradores onchange y funcion computar')

    manytoone = fields.Many2one(comodel_name="product.template", string="Many to One",
                                required=False, )

    cambio_realizado = fields.Boolean(string="Cambios en Many to One")

    @api.onchange('manytoone')
    def _detectar_cambio(self):
        self.cambio_realizado = True

    onetomany = fields.One2many(comodel_name="sale.order", inverse_name="manytoone",
                                    string="One to Many", required=False, )
    manytomany = fields.Many2many(comodel_name="sale.order",
                                     relation="sale_handler",
                                  column1="order_id", column2="order_handler_id",
                                  string="Many to Many", )

    @api.depends('computation')
    def computer_function(self):
        for record in self:
            record.computation = 300

    computation = fields.Float(string="Result: ", compute=computer_function, readonly=True)

<odoo>
<!-- Inherit Form View to Modify it -->
<record id="custom_view_custom" model="ir.ui.view">
    <field name="name">custom.view.custom</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">

        <xpath expr="//sheet" position="inside">
            <group>
            <field name="manytoone"/>
            <field name="onetomany"/>
            <field name="manytomany"/>
            </group>
            <group>
                <field name="computation"/>
                <field name="cambio_realizado"/>
            </group>
        </xpath>

    </field>
</record>

</odoo>

2 个答案:

答案 0 :(得分:0)

@ api.one def computer_function(self):     self.computation = 243 + 234

computation = fields.Float(string="Result: ", compute=computer_function, readonly=True)

在代码中使用@ api.one

答案 1 :(得分:0)

1- onchnage,当您尝试创建新记录时,onchange会自动调用,然后cambio_realizado设置为true

@api.onchange('manytoone')
def _detectar_cambio(self):
    for o in self:
        if o.manytoone:
            o.cambio_realizado = True

2-在计算中,最好使用api.depends('somefield'),但是下面的代码可能对您有用,使用循环而不使用只读

@api.multi
def computer_function(self):
    for o in self:
        o.computation = 243+234

computation2 = fields.Float(string="Result: ", compute=computer_function)