odoo - 显示2个字段的many2one字段组合的名称

Tho*_*asS 7 python python-2.7 odoo odoo-8

在我的模块中,我有以下many2one字段: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')

其中,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 %')
    }
Run Code Online (Sandbox Code Playgroud)

我知道many2one字段将名称字段作为其显示名称,但我希望它使用nameinsurance_percentage的形式的组合name + " - " + insurance_percentage + "%"

我读过最好覆盖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
Run Code Online (Sandbox Code Playgroud)

并将其置于ÌnsuranceType`类中.由于什么都没发生:我是否必须将它放在包含该字段的主类中?如果是这样,还有其他方法可以做到这一点,因为这可能也会改变其他many2one字段的显示方式吗?

for*_*vas 12

如果您不想更改many2one与模型相关的其余部分的显示名称,则xx.insurance.type可以在XML视图中将上下文添加到many2one要修改其显示名称的位置:

<field name="xx_insurance_type" context="{'special_display_name': True}"/>
Run Code Online (Sandbox Code Playgroud)

然后,在你的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
Run Code Online (Sandbox Code Playgroud)