odoo-2个字段的many2one字段的显示名称 [英] odoo - display name of many2one field combination of 2 fields

查看:1022
本文介绍了odoo-2个字段的many2one字段的显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模块中,我有以下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 %')
    }

我知道many2one字段将 name 字段作为其显示名称,但我希望它以name + " - " + insurance_percentage + "%"的形式使用nameinsurance_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

并将其放在ÌnsuranceType`类中. 由于什么也没发生: 我是否必须将其放置在包含该字段的主类中?如果是这样,还有其他方法可以执行此操作,因为这也可能会更改其他many2one字段的显示方式?

解决方案

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

<field name="xx_insurance_type" context="{'special_display_name': True}"/>

然后,在您的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

In my module i have the following many2one field: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')

where xx.insurance.type is the following:

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 + "%"

I read it is best to overwrite the get_name method so I tried the following:

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. 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?

解决方案

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:

<field name="xx_insurance_type" context="{'special_display_name': True}"/>

And then, in your name_get function:

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

这篇关于odoo-2个字段的many2one字段的显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆