Search code examples
xmlbuttonodoo-13

Fill the date field when clicking on button


I want to change the value of field ( that changes to the date of the day for example), when i click on an existing button and access to that form or tree in odoo 13.

Thanks a lot.


Solution

  • You can search the record before retrieve the form view, update it and then retrieve the view normally.

    I hope this answer can be helpful for you.

    EDITED 2022-02-06: Example.

    def action_your_button(self):
        self.ensure_one()
        # Define a proper domain to find the target record
        domain = [('your_field', '=', 'some_value')]
        record = self.env['your.model'].search(domain, limit=1)  # limit=1 to ensure update only 1 record
        # Update record
        record.write({'field_to_update': new_value})
    
        action = {
            'name': 'Action Name',
            'type': 'ir.actions.act_window',
            'res_model': 'your.model',
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': self.env.ref('your_module_name.your_view_form_id').id,
            'res_id': record.id,
            'target': 'new'
        }
        return action