Make button readonly based on a parent field

I want to make a button readonly based on a parent field. Normally you would do:

states = {
    'readonly': Eval('_parent_sale', {}).get('state') != 'draft'
}

Now I want to do the same with a button, but that isn’t working.

@classmethod
def __setup__(cls):
    super().__setup__()

    cls._buttons.update({
        confirm: {
            'readonly': Eval('_parent_sale', {}).get('state') != 'draft'
         }
    })

Am I doing something wrong or is this a limitation? Or is it possible to add this to the XML?

In such case the best is to have a function field (which depends on the parent) to compute the state and then make your button depend on such field.

Indeed, following your example we no more use _parent_sale.state but we have a sale_state field on the line.

This has the benefit that the button have the right state also when used outside of the _parent model. For example on a view that just show lines.

1 Like

Yeah, I already was afraid of that. So I added a Function field.

It should work if you are inside a One2Many for which the parent is sale.

I’m working with a Many2One as parent. So I want to look at the state field of the parent.

A Many2One does not create a parent/children relation, only the One2Many.
So the Function field is the solution.

Thanks! That’s something to remember.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.