# Setting a button active/readonly on inventory moves for a specific state

**URL:** https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318
**Category:** Developer
**Created:** [July 3, 2023, 1:06pm UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318 "2023-07-03T13:06:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [July 3, 2023, 1:06pm UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/1 "2023-07-03T13:06:28Z")

</div>

Hi,

We allow users to do a “pick” on inventory moves on customer shipment (custom code). So, we add a button ‘pick’ on the model ‘stock.move’. Those button should be only active/visible when move is in state ‘assigned’ and move is an inventory move.

 ![2023-07-03_10-04](https://discuss-cdn.tryton.org/uploads/default/original/2X/6/650064e8d948e23797fd34eaa5310a9d265a97c5.png)

So i define a function field to test if move is an inventory move out:

```auto
class Move(metaclass=PoolMeta):
    __name__ = 'stock.move'
...
    is_inventory_move_out = fields.Function(
        fields.Boolean("Is Inventory Move Out"),
        'on_change_with_is_inventory_move_out')

    @classmethod
    def __setup__ (cls):
        super(). __setup__ ()
        cls._buttons.update({
                'pick': {
                    'readonly': (~Eval('state').in_(['assigned'])
                        | ~Eval('is_inventory_move_out', False)),
                    'depends': ['state', 'is_inventory_move_out'],
                    },
                })

    @fields.depends('shipment')
    def on_change_with_is_inventory_move_out(self, name=None):
        if self.shipment:
            if getattr(self.shipment, 'warehouse_output', None):
                return self.shipment.warehouse_output == self.to_location
        return False

    @classmethod
    @ModelView.button_action('preciball.wizard_pick')
    def pick(cls, moves):
        pass

```

Everything seems ok except sometimes button stays ‘readonly’ (Pyson conditions are ok) and user has to close-reopen the customer shipment tab to get the button active.

Any idea why sometimes ‘readonly’ state of button is incorrect ?

Do I need to use another method to test if the current move is an inventory move out ? (I guess this is the function field which makes the condition sometimes wrong).

---

<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: [July 3, 2023, 4:19pm UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/2 "2023-07-03T16:19:04Z")

</div>

> [@maxx](#):
>
> Any idea why sometimes ‘readonly’ state of button is incorrect ?

I guess the button is readonly once you modified something from the parent record.  
This is the spected behaviour as you need to save first the parent record before triggering the button on some of the moves.

---

<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: [July 3, 2023, 5:24pm UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/3 "2023-07-03T17:24:43Z")

</div>

The inventory moves is a One2Many that is not using the `shipment` as field so the `on_change` is not called if something change on the parent and the `warehouse_output` is not sent (because it is not a `_parent_shipment.warehouse_output`).

So I guess it will be more stable if you could use another criteria than the shipment.

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [July 4, 2023, 6:31am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/4 "2023-07-04T06:31:05Z")

</div>

> [@ced](#):
>
> So I guess it will be more stable if you could use another criteria than the shipment.

What criteria could I use to distinct different moves ?

> [@pokoli](#):
>
> I guess the button is readonly once you modified something from the parent record.

It’s the inverse, the button is readonly by default and become active once the move is assigned. So the major condition to change the state of the button is the state of the move. My function field allows me to identify what kind of move to prevent to have an active button everywhere moves are displayed. Once the move is linked to the shipment, shipment field stays unchanged.

---

<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: [July 4, 2023, 6:33am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/5 "2023-07-04T06:33:15Z")

</div>

> [@maxx](#):
>
> What criteria could I use to distinct different moves ?

Why not just the type of locations?

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [July 4, 2023, 6:36am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/6 "2023-07-04T06:36:53Z")

</div>

> [@ced](#):
>
> Why not just the type of locations?

If “From Location” type is “Storage” then it’s always an inventory move ?

---

<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: [July 4, 2023, 6:38am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/7 "2023-07-04T06:38:28Z")

</div>

The “To Location” must also be a storage.

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [July 4, 2023, 7:15am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/8 "2023-07-04T07:15:33Z")

</div>

And in my case “To Location” must be the “Output Zone”, so self.to\_location == self.to\_location.warehouse.output\_zone

---

<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: [July 4, 2023, 7:50am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/9 "2023-07-04T07:50:23Z")

</div>

Is it really necessary? I think you can pick also for internal moves.

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [July 4, 2023, 7:51am UTC](https://discuss.tryton.org/t/setting-a-button-active-readonly-on-inventory-moves-for-a-specific-state/6318/10 "2023-07-04T07:51:40Z")

</div>

Indeed. Thanks. 🙂
