The business logic layer
Now we will add some logic to our buttons. This is done with Python code, using the methods in the model's Python class.
Adding business logic
We should edit the todo_model.py
Python file to add to the class the methods called by the buttons. First, we need to import the new API, so add it to the import statement at the top of the Python file:
from odoo import models, fields, api
The action of the Toggle Done button will be very simple: just toggle the Is Done? flag. For logic on records, use the @api.multi
decorator. Here, self
will represent a recordset, and we should then loop through each record.
Inside the TodoTask
class, add this:
@api.multi def do_toggle_done(self): for task in self: task.is_done = not task.is_done return True
The code loops through all the to-do task records and, for each one, modifies the is_done
field, inverting its value. The method does not need to return anything, but we should have it to at least return a True...