# Make button readonly based on a parent field

**URL:** https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039
**Category:** Developer
**Created:** [February 23, 2022, 4:53pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039 "2022-02-23T16:53:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [February 23, 2022, 4:53pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/1 "2022-02-23T16:53:24Z")

</div>

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

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

```

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

```auto
@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?

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [February 23, 2022, 5:05pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/2 "2022-02-23T17:05:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [February 23, 2022, 6:51pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/3 "2022-02-23T18:51:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 23, 2022, 7:09pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/4 "2022-02-23T19:09:10Z")

</div>

> [@edbo](#):
>
> Am I doing something wrong or is this a limitation? Or is it possible to add this to the XML?

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

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [February 23, 2022, 7:27pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/5 "2022-02-23T19:27:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 23, 2022, 7:32pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/6 "2022-02-23T19:32:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [February 23, 2022, 7:40pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/7 "2022-02-23T19:40:47Z")

</div>

> [@ced](#):
>
> A `Many2One` does not create a parent/children relation, only the `One2Many`.

Thanks! That’s something to remember.

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [March 25, 2022, 7:41pm UTC](https://discuss.tryton.org/t/make-button-readonly-based-on-a-parent-field/5039/8 "2022-03-25T19:41:18Z")

</div>

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