Search code examples
loopsmodelodoo-9

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.

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' object has no attribute '_ids'" while parsing

This is how I call the method:

<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

My model:

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

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() takes exactly 1 argument (3 given)"

But also does not tell much.


Solution

  • use new api

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

    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])