Get Value from Integer field on button click?



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 ?

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').

    @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'

My bad the decorator for a button instance is @ModelView.button_change which replace the @fields.depends and the @ModelView.button.

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

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

i tried this but it didnt worked

     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

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

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

self.treatment_input_qty_percent = self.on_change_with_treatment_input_qty_percent()

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

i tried this but methods is an unexpected argument for button_change

button_change() got an unexpected keyword argument 'methods'

You need a version with Issue 9871: Add methods to ModelView.button_change - Tryton issue tracker