OpenERP one2many 默认值

OpenERP one2many default value

我正在尝试修改一个名为 mrp_bom_history 的 OpenERP 插件,以从现有的 bom 行中获取默认值作为默认值。

我已经为 return 一个类似于 how to initialize a default one2many fields in OpenERP 的 bom_line id 列表创建了一个 _read_line 方法,但是我得到了一个 'Record not correctly loaded' 异常,并且我无法弄清楚原因。

Python 代码来自 save_bom_history.py:

class save_bom_history(osv.osv_memory):
    def _read_line(self,cr,uid,context=None):
        bom = self.pool.get('mrp.bom').browse(cr,uid,context['active_id'])
        result = []
        for lines in bom.bom_lines:
            result.append(lines.id)
        return result

    _name       = "save.bom.history"
    _columns    = {
                   'name'           : fields.char('Name'),
                   'cut_off_date'   : fields.date('Cut-off Date'),
                   'new_bom_ids'    : fields.one2many('save.bom.history.line','\
wizard_id','New Bill of Material'),
                   }

    _defaults   = {
                   'name'           : "History",
                   'cut_off_date'   : time.strftime('%Y-%m-%d'),
                   'new_bom_ids'    : _read_line,
                  }

One2many 和 Many2many 使用一种特殊的 "commands" 格式来操作存储在 in/associated 字段中的记录集。

试试这个:

result.append((0, 0, lines.id))

我通过返回一个元组而不是仅仅返回 id

来让它工作
        for lines in bom.bom_lines:
        line_data = {
                         'name'             : lines.name,
                         'date_start'       : lines.date_start,
                         'date_stop'        : time.strftime('%Y-%m-%d'),
                         'product_qty'      : lines.product_qty,
                         'product_id'       : lines.product_id and lines.product_id.id or False,
                         'product_uom'      : lines.product_uom and lines.product_uom.id or False,
                         'bom_id'           : bom and bom.id or False,
                         }
        result.append((0,0,line_data))
    return result