[英]odoo - display name of many2one field combination of 2 fields
In my module i have the following many2one field: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')
在我的模块中,我有以下 many2one 字段: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')
where xx.insurance.type
is the following:其中xx.insurance.type
如下:
class InsuranceType(osv.Model):
_name='xx.insurance.type'
_columns = {
'name' : fields.char(size=128, string = 'Name'),
'sale_ids': fields.one2many('sale.order', 'xx_insurance_type', string = 'Sale orders'),
'insurance_percentage' : fields.float('Insurance cost in %')
}
I know the many2one field takes the name field as its display name but I would like to have it use the combination of name
and insurance_percentage
in the form of name + " - " + insurance_percentage + "%"
我知道 many2one 字段将name字段作为其显示名称,但我希望它以name + " - " + insurance_percentage + "%"
的形式使用name
和insurance_percentage
的组合
I read it is best to overwrite the get_name
method so I tried the following:我读到最好覆盖get_name
方法,所以我尝试了以下操作:
def get_name(self,cr, uid, ids, context=None):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = []
for record in self.browse(cr, uid, ids, context=context):
name = record.name
percentage = record.insurance_percentage
res.append(record.id, name + " - " + percentage + "%")
return res
and placed this inside the ÌnsuranceType` class.并将其放在 ÌnsuranceType` 类中。 Since nothing happened: Do i have to place it inside the main class containing the field?由于什么也没发生:我是否必须将它放在包含该字段的主类中? If so, is there an other way to do this since that will probably also change the display ways of the other many2one fields?如果是这样,是否还有其他方法可以做到这一点,因为这也可能会改变其他 many2one 字段的显示方式?
If you don't want to alter the display name of the rest of the many2one
related to the model xx.insurance.type
, you can add a context in the XML view to the many2one
whose display name you want to modify:如果你不想改变的其余部分的显示名称many2one
相关模型xx.insurance.type
,您可以在XML视图中的添加上下文many2one
要修改其显示名称:
<field name="xx_insurance_type" context="{'special_display_name': True}"/>
And then, in your name_get
function:然后,在您的name_get
函数中:
def name_get(self, cr, uid, ids, context=None):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = []
if context.get('special_display_name', False):
for record in self.browse(cr, uid, ids, context=context):
name = record.name
percentage = record.insurance_percentage
res.append(record.id, name + " - " + percentage + "%")
else:
# Do a for and set here the standard display name, for example if the standard display name were name, you should do the next for
for record in self.browse(cr, uid, ids, context=context):
res.append(record.id, record.name)
return res
@api.depends('name', 'insurance_percentage')
def name_get(self):
res = []
for record in self:
name = record.name
if record.insurance_percentage:
name = '[' + record.insurance_percentage+ ']' + name
res.append((record.id, name))
return res
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.