odoo - 显示2个字段

时间:2015-07-30 13:02:26

标签: python python-2.7 odoo-8 odoo

在我的模块中,我有以下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 字段作为其显示名称,但我希望它以nameinsurance_percentage的形式使用name + " - " + insurance_percentage + "%"get_name的组合。 {1}}

我认为最好覆盖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 方法,所以我尝试了以下内容:

function AddToBag() 
    {
        var url = "/Home/Populate_Entitle/";
        $.ajax({
            url: url,
            data: { id: 1 },
            cache: false,
            type: "POST",
            success: function (data) {
                alert("hi");
                $("#gridContent").html(markup);
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });

    }

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

2 个答案:

答案 0 :(得分:12)

如果您不想更改与模型many2one相关的其余xx.insurance.type的显示名称,则可以在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

答案 1 :(得分:0)

@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