Odoo 8:是否有从 stock.picking 到 purchase.order 的 link?

Odoo 8 : Is there a link from stock.picking to purchase.order?


purchase.order 中有这个字段:

'picking_ids': fields.function(_get_picking_ids, method=True, type='one2many', relation='stock.picking', string='Picking List', help="This is the list of receipts that have been generated for this purchase order.")

好像是one2many的字段,所以在stock.picking里面有counter是符合逻辑的,但是没有,没有counter。
_get_picking_ids 方法中的代码如下所示:

def _get_picking_ids(self, cr, uid, ids, field_names, args, context=None):
    res = {}
    for po_id in ids:
        res[po_id] = []
    query = """
    SELECT picking_id, po.id FROM stock_picking p, stock_move m, purchase_order_line pol, purchase_order po
        WHERE po.id in %s and po.id = pol.order_id and pol.id = m.purchase_line_id and m.picking_id = p.id
        GROUP BY picking_id, po.id

    """
    cr.execute(query, (tuple(ids), ))
    picks = cr.fetchall()
    for pick_id, po_id in picks:
        res[po_id].append(pick_id)
    return res

有人能解释一下为什么吗?以及如何在不添加额外字段的情况下 link purchase.order 和 stock.picking? (因为数据库从 2k15 开始就已经投入生产了)。

您可以在 Odoo 上搜索 purchase_stock 模块具有 "stock.picking" 到 "purchase.order" 字段名称的链接 purchase_id

谢谢

该字段是经过计算的,因此不需要像 "real" one2many 字段中那样的计数器部分。但是您可以尝试在 stock.picking.

上从 purchase.order 反转字段 picking_ids
def _get_purchase_order_ids(self, cr, uid, ids, field_names, args, context=None):
    res = {}
    for pick_id in ids:
        res[pick_id] = []
    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()
    for pick_id, po_id in picks:
        res[pick_id].append(po_id)
    return res


'purchase_order_ids': fields.function(
    _get_purchase_order_ids, method=True, type='one2many',
    relation='purchase.order', string='Purchase Order List')

这只是一个快速示例,未经测试。