Odoo-无法遍历模型记录 [英] Odoo - Cannot loop through model records
问题描述
每次模块安装或更新时,我都想调用一个方法.在该方法内部,我想遍历模型记录,但是只会遇到不同的错误.
I want to call a method every time my module gets installed or updated. Inside that method I want to loop through model records, but I'm only getting different errors.
此文档看起来非常简单: https://www.odoo.com /documentation/9.0/reference/orm.html
This documentation looks pretty straightforward: https://www.odoo.com/documentation/9.0/reference/orm.html
但这对我不起作用.我收到此错误:
But it doesn't work for me. I'm getting this error:
ParseError:'account.tax'对象在解析时没有属性'_ids'"
ParseError: "'account.tax' object has no attribute '_ids'" while parsing
这就是我所说的方法:
<openerp>
<data>
<function model="account.tax" name="_my_method" />
</data>
</openerp>
I took this from the first answer here: 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"),则错误会有所不同:
It is basically a copy-paste from the docs. I only added method parameters cr, uid,.. If I take them away (and just leave 'self'), the error is a little different:
ParseError:"_ my_method()恰好接受1个参数(给定3个参数)"
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])
but you can keep it like that and do a pool on your model than loop throw the result that you get don't use self
if you use the new api use : self.env['model_name'].search([domain])
这篇关于Odoo-无法遍历模型记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!