In this recipe, we will manually create a record of the library.return.wizard model. We want the onchange method to compute the returned books for us. To do this, you need to perform the following steps:
- Create the return_this_books method in the library.book model:
@api.multi def return_all_books(self): self.ensure_one()
- Get an empty recordset for library.return.wizard:
wizard = self.env['library.return.wizard']
- Prepare the values to create a new wizard record. Here, we will use a current user's partner ID as the borrower_id, but if you want to put this button on the res.parnter model, you can use self.id:
values = {
'borrower_id': self.env.user.partner_id.id,
}
- Retrieve the onchange specifications for the wizard:
specs = wizard._onchange_spec()
- Get the results of the onchange method:
updates = wizard.onchange(values, ['borrower_id'], specs)
- Merge these results with the values of the new wizard:
value = updates.get('value', {})
for name, val in value.items():
if isinstance(val, tuple):
value[name] = val[0]
values.update(value)
- Create the wizard:
wiz = wizard.create(values)
return wiz.sudo().books_returns()