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

Extending Python methods

Python methods can also be extended for additional business logic. This is done by borrowing the mechanism Python already provides for inherited objects to extend their parent class behavior: super().

As an example, we will have the TodoTask class extend the ORM write() method to add some logic to it: when writing on a non-active record, it will automatically reactivate it.

Add this additional method in the models/todo_task_model.py file:

@api.multi 
def write(self, values): 
    if 'active' not in values: 
        values['active'] = True 
    super().write(values)

The write() method expects an additional parameter, with a dictionary of values to write on the record. Unless a value is being explicitly set on the active field, we set it to True in case we are writing on an inactive record. We then use super() to call the parent write() method using the modified values.

In previous Odoo versions using Python 2.7, super() needed two arguments, passing the class name and self. In that case, the last statement would be
super(TodoTask, self).write(values). This syntax also works with Python 3, so it should be preferred if keeping Python 2 compatibility is important.

We will have a chance to have a closer look at the ORM methods in Chapter 6, The ORM - Handling Application Data.

主站蜘蛛池模板: 馆陶县| 盐亭县| 广州市| 锦州市| 卢龙县| 桃江县| 安乡县| 和顺县| 彭山县| 河间市| 来凤县| 松溪县| 泾阳县| 浮梁县| 瓮安县| 肥乡县| 获嘉县| 四子王旗| 武宣县| 岳普湖县| 余江县| 河源市| 娱乐| 绥化市| 仙游县| 庆云县| 保山市| 昌黎县| 陇川县| 双流县| 盐边县| 遂川县| 英德市| 湄潭县| 咸阳市| 双江| 肇庆市| 昭通市| 阜宁县| 宜章县| 鄂托克旗|