python - Odoo 中的递归编程

标签 python odoo 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 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)

所以它不会无限递归。

关于python - Odoo 中的递归编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423391/

相关文章:

python - 如何在 pyspark 2.3 中的二元问题(BinaryClassificationEvaluator)中为 CrossValidator 评估器使用 f1-score

python - 搜索重合数据时避免排列

python - 如何区分两个日期时间字段结果的小时数,odoo?

javascript - 如何在保存模式下打开向导操作也隐藏编辑、创建、保存和丢弃按钮 Odoo 11

odoo - 如何从 ODOO 10 中的销售订单创建交货单?

nginx - 异常 : bus. 使用 nginx 作为代理服务器的 Odoo 8 多处理总线不可用

python - 如何在django-sphinx下自动重建Sphinx索引?

Visual Studio 中的 Python 2.7.11 在 cElementTree.py 中抛出异常

xml - 如何在odoo的电子邮件模板中添加来自其他模型的字段?

opencv - 如何在看板 View 中显示多多字段,没有颜色标签