python-2.7 - 如何从 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),
}
解决方案
你已经有product_id
字段了stock.quant
,你可以做designation2
相关的product_id
。
designation2 = fields.Char(related='product_id.name')
要在 和 之间添加关系purchase.order
, 您可以使用从字段stock.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
推荐阅读
- c# - 错误 - 发布后输入字符串的格式不正确
- python-3.x - 如何创建一个分数计数器,以便它从循环中添加一个值
- django - ModuleNotFoundError:运行 manage.py 时没有名为“django”的模块
- docker - 如何完全停止 dockerd 使用 cgroups?
- java - 从 Cassandra 读取数据时出现 TraceRetrievalException
- markdown - 为使用反引号指定的特定降价代码列表启用行号
- ios - Swift 默认的 AlertViewController 打破约束
- excel - 如何通过marco将带有Excel工作表的电子邮件发送给多个收件人,只需稍作修改
- unity3d - 生存射击敌人不会增加分数
- javascript - 如何在Angular中异步获取对象的一些属性