Skip to main content
 首页 » 编程设计

python之Odoo 中的递归编程

2025年02月15日10zfyouxi

我们可以在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 中启用递归?

请您参考如下方法:

原来发生错误是因为我在递归中使用了 interactive python debugger 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) 

所以它不会无限递归。