跳至内容
菜单
此问题已终结
2 回复
39091 查看

Hi everyone,

I am new to Odoo development and need to know what is the difference between @api.model and @api.multi and if possible, also tell me when/where to use each of them?

Thank you in advance

Regards

Paulo    

形象
丢弃
最佳答案

Hi,


@api.multi :-

Self will be the current RecordSet without iteration. It is the default behavior:

@api.multi
def afun(self):
    len(self)


@api.model:-

This decorator will convert old API calls to decorated function to new API signature. It allows to be polite when migrating code.

@api.model
def afun(self):
    pass

Please see this link: https://odoo-new-api-guide-line.readthedocs.io/en/latest/decorator.html

Thanks

形象
丢弃
编写者

Great. Thank you very much @Cybrosys

最佳答案

@api.multi :-

The decorator in default for methods from new api onwards, which the self contains a recordset. So operations should be done by iterating through the recordsets if it is a set.

eg:

@api.multi
def multi_method(self):
    """ """
    print self # [model(1,2,3,4)] or [model(1,)]

@api.model :-

The decorator used on methods where the contents are not relevant. No ids will get passed with such methods.

eg:

@api.model
def create(self, vals)
    """ """
    print self # recordset with no id



形象
丢弃
编写者

Great. Thank you very much @Hilar

相关帖文 回复 查看 活动
1
3月 22
31329
3
10月 23
9486
1
9月 23
3632
1
5月 23
2544
2
4月 23
3177