orm api 装饰器

one装饰器的作用是对每一条记录都执行对应的方法,无返回值。

model装饰器的作用是返回一个集合列表,一般用来定义自动化动作里面,该方法无ids传入。

@api.model
def some_method(self, a_value):
    pass
# can be called as
old_style_model.some_method(cr, uid, a_value, context=context)
constrains装饰用于对字段进行限制

@api.constrains('age')
def _check_age(self):
    if self.age<16:
        raise ValueError(_('Age must be older than 16'))
multi装饰器的作用是对每一条记录都执行对应的方法,与one装饰类不同的是有返回值。

depends装饰  
  
1.在计算字段值(而不是直接从数据库中读取)时使用的计算参数。它必须将计算值分配给该字段。如果使用其他字段的值,应该使用depends()指定这些字段  
实例: 

    total=fields.Float(compute=“compute_total”)  
@api.depends('value','tax)    
    def compute_total(self):  
        for record in self:  
            record.total=record.value+record.value*record.tax  
2. 当使用关联字段是可以指名路径:  
实例:

    @api.depends('line_ids.values')  
    def compute_total(self):  
        for record in self:  
            record.total=sum([line.value for line in record.line_ids]) 

onchange装饰  
当用户更改某个字段的值时(但尚未保存该表单),他可以自动更改基于该字段的字段值,如更改或添加一个新的发票行时,该值自动更新 

returns()装饰   
返回一个对象的集合,第一个参数是记录的模型名字或是self(当前模型)

>>> @api.multi
... @api.returns('self')
... def some_method(self):
...     return self
>>> new_style_model = env['a.model'].browse(1, 2, 3)
>>> new_style_model.some_method()
a.model(1, 2, 3)
>>> old_style_model = pool['a.model']
>>> old_style_model.some_method(cr, uid, [1, 2, 3], context=context)
[1, 2, 3]
One2many,Many2many使用特殊的“命令”格式来操作设置的字段。这种格式是三元的,(This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. Not all commands apply in all situations.) Possible commands are:
(0, _, values)
从所给value的字典中添加一条新纪录
(1, id, values)
更新一条已有记录,id为:id,更新内容为values。Can not be used in create().
(2, id, _)
移走id为“id”的记录,并从数据库中删除。Can not be used in create().
(3, id, _)
移走id为“id”的记录,但并不从数据库中删除。 Can not be used on One2many. Can not be used in create().
(4, id, _)
向id为“id”记录处添加已有记录。 Can not be used on One2many.
(5, _, _)
从记录集中删除所有数据, Can not be used on One2many. Can not be used in create().
(6, _, ids)
用ids列表中的所有记录替换原有记录。 Can not be used on One2many.


注:标记为“_”的值可以为任意值,通常为“0”或“False”。

猜你喜欢

转载自blog.csdn.net/miantian180/article/details/81697742
Orm