xmlodoostatefieldreadonly

How to make two related fields have different readonly behavior depending on state in odoo 18?


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?


Solution

  • 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