官术网_书友最值得收藏!

Adding business logic

We should edit the todo_task_model.py Python file to add the methods called by the button. 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 api, fields, models 

The logic for the Clear Done button is quite simple: just set the Active? flag to false. This takes advantage of an ORM built-in feature: records with an active flag set to False by default are filtered out and won't be presented to the user. You can think of it as a "soft delete."

In the models/todo_task_model.py file, add the following to the TodoTask class:

@api.multi 
def do_clear_done(self): 
    for task in self: 
        task.active = False 
    return True 

For logic on records, we use the @api.multi decorator. Here, self will represent a recordset, and we should then loop through each record. In fact, this is the default for Model methods, so the @api.multi decorator can safely be omitted here. We prefer to keep it for clarity.

The code loops through all the To-Do task records and, for each one, assigns the False value on the active field.

The method does not need to return anything, but we should have it at least return a True value. The reason is that not all client implementations of the XML-RPC protocol support None/Null values, and may raise errors when such a value is returned by a method.

主站蜘蛛池模板: 九寨沟县| 隆子县| 东阳市| 宁强县| 乌鲁木齐县| 凭祥市| 广东省| 弥勒县| 加查县| 麻江县| 荣昌县| 炎陵县| 龙泉市| 达州市| 禹州市| 福鼎市| 阳高县| 金塔县| 峡江县| 红桥区| 湘潭县| 二手房| 济源市| 额尔古纳市| 女性| 张家界市| 武川县| 榆林市| 武穴市| 剑阁县| 沧州市| 明水县| 正定县| 修武县| 彭山县| 贞丰县| 游戏| 黄浦区| 蓬莱市| 漯河市| 长兴县|