Odoo 9 如何正确连接两个字符?
Odoo 9 how to correctly concat two chars?
我有 3 个字段并尝试连接字段,所以 char3=char1+char2
编写了以下代码:
# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.tools import ustr
class pucrhase_order_pref_supplier(models.Model):
_inherit = 'purchase.order.line'
#this field will be displayed on the product list in the purchase order
preferred_supplier_product = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
preferred_supplier_template = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
preferred_supplier = fields.Char(compute='_onchange_proc', store="True")
@api.one
@api.depends('preferred_supplier','preferred_supplier_product','preferred_supplier_template')
def _onchange_proc(self):
string1 = self.preferred_supplier_product
string2 = self.preferred_supplier_template
output = ustr(string1)+"-"+ustr(string2)
self.preferred_supplier = output
不确定为什么 preferred_supplier 未计算(其他字段工作正常)。我应该改用 onchange 吗?
depends 中的字段列表不应包含与计算相同的字段。并且不依赖于相关领域使用DOT在其他模型中更深入
# no need to create related fields if you are not showing them in your form view
@api.depends('product_id', 'product_id.preferred_supplier_middle',
'product_id.preferred_supplier_middle')
def _onchange_proc(self):
# I prefer using string formatting for this kind of work
self.preferred_supplier = '%s-%s' % (product_id.preferred_supplier_middle or '', product_id.preferred_supplier_middle or '')
首先是对代码本身的一些评论:
preferred_supplier_product 和 preferred_supplier_template 与完全相同的领域相关,我认为不应该。
正如 Cherif 所说,如果您甚至不需要视图中的字段,则无需创建它们
我将使用 Python True / False 作为字段的 readonly/store 属性,而不是字符串。
为什么计算要靠自己?
现在为您解答:
@api.multi
@api.depends('preferred_supplier_product', 'preferred_supplier_template')
def _onchange_proc(self):
for record in self:
record.preferred_supplier = '%s-%s' % (record.preferred_supplier_product, record.preferred_supplier_template)
或者取决于您是否需要为您的视图声明的字段
@api.multi
@api.depends('product_id.preferred_supplier_middle')
def _onchange_proc(self):
for record in self:
record.preferred_supplier = '%s-%s' % (
record.product_id.preferred_supplier_middle, record.product_id.preferred_supplier_middle)
您可以通过以下方法连接两个字符串:
@api.onchange('char1', 'char2')
def _onchange_weight_range(self):
list = []
if self.char1:
conc = str(self.char1 or '') + '-' + str(self.char2 or '')
list.append(conc)
self.char3 = ', '.join(list)
我有 3 个字段并尝试连接字段,所以 char3=char1+char2
编写了以下代码:
# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.tools import ustr
class pucrhase_order_pref_supplier(models.Model):
_inherit = 'purchase.order.line'
#this field will be displayed on the product list in the purchase order
preferred_supplier_product = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
preferred_supplier_template = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
preferred_supplier = fields.Char(compute='_onchange_proc', store="True")
@api.one
@api.depends('preferred_supplier','preferred_supplier_product','preferred_supplier_template')
def _onchange_proc(self):
string1 = self.preferred_supplier_product
string2 = self.preferred_supplier_template
output = ustr(string1)+"-"+ustr(string2)
self.preferred_supplier = output
不确定为什么 preferred_supplier 未计算(其他字段工作正常)。我应该改用 onchange 吗?
depends 中的字段列表不应包含与计算相同的字段。并且不依赖于相关领域使用DOT在其他模型中更深入
# no need to create related fields if you are not showing them in your form view
@api.depends('product_id', 'product_id.preferred_supplier_middle',
'product_id.preferred_supplier_middle')
def _onchange_proc(self):
# I prefer using string formatting for this kind of work
self.preferred_supplier = '%s-%s' % (product_id.preferred_supplier_middle or '', product_id.preferred_supplier_middle or '')
首先是对代码本身的一些评论: preferred_supplier_product 和 preferred_supplier_template 与完全相同的领域相关,我认为不应该。
正如 Cherif 所说,如果您甚至不需要视图中的字段,则无需创建它们
我将使用 Python True / False 作为字段的 readonly/store 属性,而不是字符串。
为什么计算要靠自己?
现在为您解答:
@api.multi
@api.depends('preferred_supplier_product', 'preferred_supplier_template')
def _onchange_proc(self):
for record in self:
record.preferred_supplier = '%s-%s' % (record.preferred_supplier_product, record.preferred_supplier_template)
或者取决于您是否需要为您的视图声明的字段
@api.multi
@api.depends('product_id.preferred_supplier_middle')
def _onchange_proc(self):
for record in self:
record.preferred_supplier = '%s-%s' % (
record.product_id.preferred_supplier_middle, record.product_id.preferred_supplier_middle)
您可以通过以下方法连接两个字符串:
@api.onchange('char1', 'char2')
def _onchange_weight_range(self):
list = []
if self.char1:
conc = str(self.char1 or '') + '-' + str(self.char2 or '')
list.append(conc)
self.char3 = ', '.join(list)