首页 > 解决方案 > 如何从 purchase.order 中获取产品描述的值

问题描述

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

在此处输入图像描述

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>

股票.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),

}

标签: python-2.7odoo-8

解决方案


你已经有product_id字段了stock.quant,你可以做designation2相关的product_id

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

要在 和 之间添加关系purchase.order, 您可以使用从字段stock.quant搜索的计算字段来访问在字段中存储名称(唯一)的字段。move_idquant_idspicking_idpurchase.orderorigin

# 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

推荐阅读