Odoo 8:未触发 fields.function() 中 fnct_inv 的问题

Odoo 8 : Problem with fnct_inv in fields.function() not triggered


我创建了一个具有此声明的函数字段:

'purchase_id': fields.function(_get_purchase_id, fnct_inv=_set_purchase_id, method=True, type="many2one", relation="purchase.order", string="Bon de commande"),

问题是 fnct_inv 没有被触发,字段 purchase_id 仍然是空的。
这是方法 _set_purchase_id 的定义:

def _set_purchase_id(self, cr, uid, ids, field_name, field_value, arg, context):
    query = """
    SELECT picking_id, po.id FROM stock_picking p, stock_move m, purchase_order_line pol, purchase_order po
        WHERE p.id in %s and p.id = m.picking_id and m.purchase_line_id = pol.id and pol.order_id = po.id
        GROUP BY picking_id, po.id

    """
    cr.execute(query, (tuple(ids), ))
    picks = cr.fetchall()
    _logger.info("============================================ %s " % ids)
    for pick_id, po_id in picks:
        self.write(cr, uid, [pick_id], {'purchase_id': [(4, po_id)]}, context=context)

有人可以帮助我吗?或者还有其他解决方案吗?谢谢

odoo8 中,我们使用 Computed 字段。

purchase_id = fields.Many2one('purchase.order','Bon de commande',compute='_set_purchase_id')

def _set_purchase_id(self, cr, uid, ids, field_name, field_value, arg, context):
    query = """
    SELECT picking_id, po.id FROM stock_picking p, stock_move m, purchase_order_line pol, purchase_order po
        WHERE p.id in %s and p.id = m.picking_id and m.purchase_line_id = pol.id and pol.order_id = po.id
        GROUP BY picking_id, po.id

    """
    cr.execute(query, (tuple(ids), ))
    picks = cr.fetchall()
    _logger.info("============================================ %s " % ids)
    for pick_id, po_id in picks:
        self.write(cr, uid, [pick_id], {'purchase_id': [(4, po_id)]}, context=context)

更多可以参考: https://www.odoo.com/documentation/8.0/howtos/backend.html#computed-fields-and-default-values

Odoo 中 "inverse" 方法的目的不是设置其定义的字段,而是设置该字段计算所需的所有字段,以便计算将计算用户的值正在为该计算字段提供 Odoo。

让我给你举个例子,因为我不想为你的问题准备一个现成的解决方案。

如果模型上的计算字段 days_since_last_action 由同一模型上的日期字段 (date_last_action) 计算,则反向方法不会设置 days_since_last_action .相反,您必须采用新的 days_since_last_action 值并将其反转为新日期。

这意味着

计算方法是days_since_last_action = today - date_last_action

逆法为date_last_action = today - new value of days_since_last_action