如何从purchase.order获得产品描述的价值

时间:2018-07-07 10:46:37

标签: python-2.7 odoo-8

我想在“ stock.quant.tree”视图中添加一个“名称”字段,以显示库存中每种产品的描述。我已经在树中添加了该字段,但是我卡住了如何将模型“ purchase.order”的描述产品的值转换为“ stock.quant”。有任何帮助的想法吗?

enter image description here

stock_quant_tree_designation.xml

<data>

<record id="stock_quant_tree_designation" model="ir.ui.view">

        <field name="name">stock.quant.tree.designation</field>

        <field name="model">stock.quant</field>

        <field name="inherit_id" ref="stock.view_stock_quant_tree"/>

        <field name="arch" type="xml">

            <xpath expr="//field[@name='qty']" position="after">

                        <field name="designation2"/>

            </xpath>

        </field>

    </record>

</data>

stock.py

from openerp.osv import fields, osv 
from openerp import tools , api
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _
class StockQuantTreeDesignation(osv.osv):
_inherit = 'stock.quant'
_columns = {
'designation2': fields.char(string="Désignation", required=True, select=True),

}

1 个答案:

答案 0 :(得分:0)

您已经在product_id中拥有stock.quant字段,可以使designation2product_id相关。

designation2 = fields.Char(related='product_id.name')

要在purchase.orderstock.quant之间添加关系,您可以使用一个计算字段,该字段从move_id字段中搜索quant_ids,以访问存储在picking_id中的{ purchase.order字段中的origin名称(唯一)。

# coding: utf8
from openerp import models, fields, api


class StockQuant(models.Model):
    _inherit = "stock.quant"

    purchase_id = fields.Many2one("purchase.order", string="Purchase Order", compute="_get_purchase_order")

    @api.multi
    def _get_purchase_order(self):
        for record in self:
            # Search for one move_id to get stock_picking reference
            self.env.cr.execute("SELECT move_id FROM stock_quant_move_rel WHERE quant_id=%s LIMIT 1", (record.id,))
            (move_id, ) = self.env.cr.fetchone()
            move = self.env["stock.move"].browse(move_id)
            # Use stock_picking to get purchase_order reference using `origin` which is unique
            po = self.env["purchase.order"].search([("name", '=', move.picking_id.origin)])
            record.purchase_id = po.id