0
votes

In my project i have an entity with a binary attribute (shape). I made an onchange method to raise a message (validationError) if the user uploads a file with the wrong extention, but i also want the method to clear the value of the binary field (cause the widget is still showing the name of the file I just upload), so the user must select a new one. I have tried with "self.shape_filename=False" but is not working. What should I do to clear the value of the binary field once Odoo raises the exception? This is my code:

@api.onchange('shape_filename')
def _onchange_shape(self):
    if self.shape_filename and not self.shape_filename.endswith('.zip'):
        self.shape=False
        self.shape_filename=False
        raise ValidationError("Shape must be a .zip file ")

and in the view

<field name="shape" widget="download_link" filename="shape_filename" options="{'filename': 'shape_filename'}"/>
   <field name="shape_filename" readonly="1"  invisible="1" force_save="1"/>

Shows how the name of the file remains in the widget

1

1 Answers

0
votes

If you raise an error all changes in the flow are reverted, to avoid data corruption when an error is unintentional.

So when you raise an error after setting the fields to False, those changes will be rolled back to the values before the method was triggered.

Instead what you want is to return a warning, which is not counted as an error, and therefor does not do a rollback. however it will still show the message, and the data should be cleared correctly.

return {
         "warning": {"title": "Warning", "message": "Shape must be a .zip file."}
}