Python Odoo中的递归编程

Python Odoo中的递归编程,python,openerp,odoo-8,Python,Openerp,Odoo 8,我们可以在Odoo的函数中使用递归吗? 在我下面的代码中 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

我们可以在Odoo的函数中使用递归吗? 在我下面的代码中

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)
我得到了在一个线程中创建的SQLite对象只能在同一个线程中使用


为什么会这样?我们如何在Odoo中启用递归呢?

事实证明,发生错误是因为我正在使用交互式python调试器
ipdb.set_trace()在递归内部

我还需要像这样纠正我的递归

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)

因此它不会无限递归。

再次调用函数时,请尝试删除self参数,该参数与您在
https://www.python.org/dev/peps/pep-0008/
thant表示序列(字符串、列表、元组),使用以下事实,即空序列为false
if not seq:或if seq:
not
if len(seq):或if not len(seq):