admin管理员组

文章数量:1332359

I have these related fields

qty_request = fields.Float('Qty Request')
product_qty = fields.Float('Product Quantity',
                           digits = 'Product Unit of Measure',
                           related = "qty_request",
                           store = True )

Which makes the product_qty automatically filled when the user enter a value on qty_request field. Now I have to make the qty_request editable in the draft state but readonly in the confirm state. While the product_qty is readonly in the draft state but editable in the confirm state.

I coded it like this in my view.xml

<field name="qty_request" readonly="state != 'draft'"/>
<field name="product_qty" string="Quantity" readonly="state != 'confirm'"/>

But the product_qty readonly behavior follows the qty_request field. I'm afraid it's because product_qty is related to qty_request so it also have its behavior. How do I solve this?

I have these related fields

qty_request = fields.Float('Qty Request')
product_qty = fields.Float('Product Quantity',
                           digits = 'Product Unit of Measure',
                           related = "qty_request",
                           store = True )

Which makes the product_qty automatically filled when the user enter a value on qty_request field. Now I have to make the qty_request editable in the draft state but readonly in the confirm state. While the product_qty is readonly in the draft state but editable in the confirm state.

I coded it like this in my view.xml

<field name="qty_request" readonly="state != 'draft'"/>
<field name="product_qty" string="Quantity" readonly="state != 'confirm'"/>

But the product_qty readonly behavior follows the qty_request field. I'm afraid it's because product_qty is related to qty_request so it also have its behavior. How do I solve this?

Share Improve this question edited Nov 21, 2024 at 4:17 Nguyễn Mạnh Cường 6082 silver badges17 bronze badges asked Nov 21, 2024 at 2:52 Osama bin MirzaOsama bin Mirza 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Just make product_qty computed instead of related (which is a shortcut computed field).

product_qty = fields.Float(
    string="Product Quantity",
    digits="Product Unit of Measure",
    compute="_compute_product_qty",
    store=True,
    readonly=True,

@api.depends("state", "qty_request")
def _compute_product_qty(self):
    for record in self:
        if record.state == "draft":
            record.product_qty = record.qty_request
        else:
            record.product_qty = record.product_qty

本文标签: