odoo-从many2one字段获得价值 [英] odoo - get value from many2one field

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

问题描述

我的代码:

class SaleOrder(osv.Model):
    _inherit = 'sale.order'

    _columns = {
        'xx_delivery_date': fields.date(string='Delivery date'),
        'xx_payment_method': fields.many2one('xx.payment.method',
                                             string='Payment method'),
        'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')
    }

    def _amount_insurance(self, cr, uid, val1, context=None):
        val = 0.0
        insurance_chosen = self.pool.get('xx.insurance.type').browse(cr, uid, insurance_percentage.id,context=context)
        val = val1*insurance_chosen/100
        return val

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 %')
    }

我正在尝试从'insurance_percentage'字段中获取浮动金额,并将此百分比添加到val1中.

I am trying to get the float from the 'insurance_percentage' field and add this percentage to val1.

此刻我的代码生成

'Global name insurance_percentage not defined,

所以我必须以某种方式告诉函数从 InsuranceType 类中获取变量,但是我不知道该怎么做.

So I have to somehow tell the function to take the variable from the InsuranceType class but I don't know how to do this.

推荐答案

对于many2one字段,我们需要首先获取该记录的ID,然后浏览具有ID的记录并从中获取期望值.

For many2one field, we need to first take id of that record and than browse that record with id and take desire value from that.

尝试使用此代码:

def _amount_insurance(self, cr, uid, ids, val1, context=None):
    val = 0.0
    for insurance in self.browse(cr, uid, ids,context=context):
        if insurance.xx_insurance_type:
            val = (val1 * (insurance.xx_insurance_type.insurance_percentage/100))
    return val

这篇关于odoo-从many2one字段获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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