odoo - get value from many2one field

老子叫甜甜 提交于 2019-12-13 12:29:36

问题


My code:

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

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

At the moment my code results in

'Global name insurance_percentage not defined,

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.


回答1:


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.

Try with this code:

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


来源:https://stackoverflow.com/questions/31680937/odoo-get-value-from-many2one-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!