strike_noir
strike_noir

Reputation: 4174

Recursive programming in Odoo

Can we use recursion in Odoo's function? In my code below

def create_lines(self, item_id=None, parent_id=None):
    source_items = self.env['product.source']
    duplicate_items = self.env['product.duplicate']
    recs = source_items.search([['parent_id', '=', item_id]])
    for rec in recs:
        value = {   'parent_id': parent_id,
                    'name': rec.name,
                    'date': rec.date,
                    'description': rec.description
                }
        line = duplicate_items.create(value)
        self.create_lines(self, rec.id, line.id)

I'm getting SQLite objects created in a thread can only be used in that same thread

Why is this happening? And how can we enable recursion in Odoo?

Upvotes: 2

Views: 860

Answers (1)

strike_noir
strike_noir

Reputation: 4174

It turned out the error happened because I'm using interactive python debugger ipdb.set_trace(); inside the recursion.

Also I need to correct my recursion like this

def create_lines(self, item_id=False, parent_id=False):
    source_items = self.env['product.source']
    duplicate_items = self.env['product.duplicate']
    recs = source_items.search([['parent_id', '=', item_id]])
    for rec in recs:
        value = {   'parent_id': parent_id,
                    'name': rec.name,
                    'date': rec.date,
                    'description': rec.description
                }
        line = duplicate_items.create(value)
        childs = source_items.search([['parent_id', '=', rec_id]])
        if (len(childs)):
            self.create_lines(self, rec.id, line.id)

So it doesn't recursive infinitely.

Upvotes: 1

Related Questions