Odoo 11 - Difference between the @api.multi and @api.model decorators(转)

10 篇文章 0 订阅

问题:

I find the documentation on this a little vague…

https://www.odoo.com/documentation/11.0/reference/orm.html#module-odoo.api

I’m not really sure which to use, as both works as expected in my
project.

Can someone please explain the difference to me with simple words? A
simplified example would be great.

回答:

as a rule of thumb you can say the following (with a few exceptions): The @api.multi call is used for functions where you want to do some operation on one or multiple records. An example here can be setting a field on multiple records:

@api.multi
def update_sale_order_dates(self):
    for order in self:
        order.your_field = 'new value'

The @api.model decorator is used for framework related usecases.
Look at @api.model as the decorator that you need to do something on the model level itself, not just some record(s). Schedulers for example use the @api.model decorator. You can use the @api.model decorator for model structures, helper methods and so on.
I hope this helps you a bit. You might also want to look through the official Odoo code as there are loads of examples there!
See https://github.com/odoo/odoo/search?q=%40api.model&unscoped_q=%40api.model and https://github.com/odoo/odoo/search?q=%40api.multi&unscoped_q=%40api.multi - usually the best way to learn is to look at the source code.

转自:https://www.odoo.com/zh_CN/forum/help-1/question/odoo-11-difference-between-the-api-multi-and-api-model-decorators-137544

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值