我使用Odoo(v10),我得到两个具有相同关系的many2one字段:
product_id(sale.order)
product_id = fields.Many2one(
'product.product', 'Product',
domain=[('type', 'in', ['product', 'consu'])], index=True, required=True,
states={'done': [('readonly', True)]})
和product_id(stock.move)
product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product')
,然后我有两个字段char:名称(product.product)和kode_produksi(product.product)
name = fields.Char(string='name')
kode_produksi = fields.Char(string='Kode Produksi')
我的目标:product_id(sale.order)将显示基于字段名称的价值,而product_id(stock.move)将显示基于字段kode_produksi的价值,有人可以帮助解决此问题吗?谢谢!
[确定,我自己解决了,做到了,尝试这段代码..
型号:
_inherit = "product.product"
@api.multi
def name_get(self):
result = []
for record in self:
if self.env.context.get('product_id', False):
# Only goes off when the custom_search is in the context values.
name = str(record.name)
result.append((record.id, "{}".format(name)))
else:
kode_produksi = str(record.kode_produksi)
result.append((record.id, kode_produksi))
return result
在该代码上下文中,默认值为False,它将在字段kode_produksi中显示数据,显示名称为many2one。如果我将xml代码放入这样:
<field name="product_id" context="{'product_id':1}" />
它将在字段名称中显示数据,显示名称为many2one