# Get Value from Integer field on button click?

**URL:** <https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568>\
**Category:** Developer\
**Created:** [August 26, 2021, 7:37pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568 "2021-08-26T19:37:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [August 26, 2021, 7:37pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/1 "2021-08-26T19:37:35Z")

</div>

```auto

class WorkType( metaclass=PoolMeta):

    __name__ = 'production.work'

   treatment_input_qty = fields.Numeric("Input qty",states={

            'invisible': ~Eval('treatment_boolean', True),

            },depends=['treatment_boolean'])

    treatment_output_qty = fields.Numeric("Output qty",states={

            'invisible': ~Eval('treatment_boolean', True),

            },depends=['treatment_boolean'])

    treatment_loss = fields.Numeric("Loss",states={

            'invisible': ~Eval('treatment_boolean', True),

            },depends=['treatment_boolean'])

    treatment_lye_collected = fields.Numeric("Lye collected",states={

            'invisible': ~Eval('treatment_boolean', True),

            },depends=['treatment_boolean'])

     @classmethod
     def __setup__ (cls):
        super(WorkType, cls). __setup__ ()
        cls._buttons.update({
            'validate_mb_treatment': {
                        'invisible': ~Eval('treatment_boolean', True),
                        'depends': ['treatment_boolean'],
                        },
            })
   
   @classmethod
   @ModelView.button
   def validate_mb_treatment(self, name=None):
        print(self.treatment_output_qty)

```

i tried the above code but `self.treatment_output_qty` returns Object instead of int value  
apart from these how can i set another field when this button is clicked ?

---

<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:** [August 26, 2021, 8:23pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/2 "2021-08-26T20:23:48Z")

</div>

If your button is a classmethod then `self` is not an instance but the class. So you should remove the decorator to get an instance button and add `@fields.depends('treatment_output_quantity')`.

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [August 27, 2021, 5:44am UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/3 "2021-08-27T05:44:33Z")

</div>

```auto
    @fields.depends('treatment_output_qty')

    @ModelView.button

    def validate_mb_treatment(self,records):

        print(int(self.treatment_output_qty))

        # sums = self.treatment_output_qty + self.treatment_loss + self.treatment_lye_collected     

        # print(sums)

```

Tried these but getting the following error  
`TypeError: wrapper() missing 1 required positional argument: 'records'`

---

<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:** [August 27, 2021, 6:32am UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/4 "2021-08-27T06:32:08Z")

</div>

My bad the decorator for a button instance is [`@ModelView.button_change`](https://docs.tryton.org/projects/server/en/latest/ref/models/models.html#trytond.model.ModelView.button_change) which replace the `@fields.depends` and the `@ModelView.button`.

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [August 27, 2021, 9:13am UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/5 "2021-08-27T09:13:08Z")

</div>

Thanks a lot it worked for me but i was wondering how can i set another field when the button is clicked ?

---

<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:** [August 27, 2021, 11:24am UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/6 "2021-08-27T11:24:01Z")

</div>

Like for on `on_change_*` you can assign the value of the field to the instance.

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [August 27, 2021, 12:27pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/7 "2021-08-27T12:27:57Z")

</div>

i tried this but it didnt worked

```auto
     treatment_input_qty_percent= fields.Numeric("Input qty",states={
            'invisible': ~Eval('treatment_boolean', True),
            },depends=['treatment_boolean'])
     
     @fields.depends(methods=['validate_mb_treatment']) 
     def on_change_with_treatment_input_qty_percent(self, name=None):
        
        return 2

    @fields.depends(methods=['on_change_with_treatment_input_qty_percent'])
    @ModelView.button_change('treatment_output_qty','treatment_input_qty', 'treatment_loss', 'treatment_lye_collected')

    def validate_mb_treatment(self):

        sums = self.treatment_output_qty + self.treatment_loss + self.treatment_lye_collected

        if self.treatment_input_qty == sums:

            self.on_change_with_treatment_input_qty_percent()

            raise UserError("Value Matched","All Value Matched")

        else :

            raise UserError("Failed","Value Does not Match")

```

The value in `treatment_input_qty_percent` is set when the form is saved

---

<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:** [August 27, 2021, 1:33pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/8 "2021-08-27T13:33:56Z")

</div>

> [@deathrake](#):
>
> ```auto
> @fields.depends(methods=['on_change_with_treatment_input_qty_percent'])
> @ModelView.button_change('treatment_output_qty','treatment_input_qty', 'treatment_loss', 'treatment_lye_collected')
> 
> ```

You must remove `@fields.depends` and add methods to `@ModelView.button_change`.

> [@deathrake](#):
>
> ```auto
> self.on_change_with_treatment_input_qty_percent()
> 
> ```

You must assign the value otherwise it does nothing. E.g:

```auto
self.treatment_input_qty_percent = self.on_change_with_treatment_input_qty_percent()

```

> [@deathrake](#):
>
> ```auto
> raise UserError("Value Matched","All Value Matched")
> 
> else :
> 
> raise UserError("Failed","Value Does not Match")
> 
> ```

You can not raise exception on `button_change` (like you can not on `on_change_*`).

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [August 27, 2021, 1:55pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/9 "2021-08-27T13:55:50Z")

</div>

> [@ced](#):
>
> add methods to `@ModelView.button_change`

i tried this but methods is an unexpected argument for button\_change

`button_change() got an unexpected keyword argument 'methods'`

---

<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:** [August 27, 2021, 2:19pm UTC](https://discuss.tryton.org/t/get-value-from-integer-field-on-button-click/4568/10 "2021-08-27T14:19:54Z")

</div>

You need a version with [Add methods to ModelView.button\_change (#9871) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue9871)
