0

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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it worked, but the readonly behavior is still kinda weird. qty_request is editable in both draft and confirm state, while product_qty is readonly in both state. Those 2 fields are inside this <page string="Components Lines"> <field name="move_line" nolabel="1" widget="one2many_list" readonly="state != 'draft' and state != 'confirm'"> <list editable="bottom"> <field name="qty_request" readonly="state != 'draft'"/> <field name="product_qty" string="Quantity" readonly="state != 'confirm'"/> </list> </field> </page> Does that readonly in move_line affects all fields inside it?
yes readonly on a x2many field in views will make all columns readonly (or not). I'm not sure if readonly="parent.state != 'draft'" directly on the field (subfield of the x2many field) will work, but you could try.
It worked! thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.