Odoo - 无法遍历模型记录

Odoo - Cannot loop through model records

我想在每次安装或更新我的模块时调用一个方法。在该方法中,我想遍历模型记录,但我只会收到不同的错误。

此文档看起来非常简单:https://www.odoo.com/documentation/9.0/reference/orm.html

但这对我不起作用。我收到此错误:

ParseError: "'account.tax' object has no attribute '_ids'" while parsing

我是这样调用方法的:

<openerp>
    <data>
        <function model="account.tax" name="_my_method" />
    </data>
</openerp>

我从这里的第一个答案中得到这个:https://www.odoo.com/forum/help-1/question/how-can-i-execute-a-sql-statement-on-module-update-and-installation-6131

我的模特:

class my_account_tax(models.Model):
    _name = 'account.tax'
    _inherit = 'account.tax'

    def _my_method(self, cr, uid, ids=None, context=None):
        self.do_operation()

    def do_operation(self):
        print self
        for record in self:
            print record

它基本上是文档的复制粘贴。我只加了方法参数cr,uid,..如果我把它们拿走(只留下'self'),错误有点不同:

ParseError: "_my_method() takes exactly 1 argument (3 given)"

不过也说不多。

使用新的api

@api.multi    #if you use the new api you don't have to list all parameter in the function
def _my_method(self): 

但是你可以保持这样,在你的模型上做一个池而不是循环抛出你得到的结果不要使用 self 如果您使用新的 api 使用:self.env['model_name'].search([domain])