2015-07-28 3 views
3

Моего кода: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» поля и добавить процент к знач1.

В данный момент мои результаты кода в

'Global name insurance_percentage not defined, 

Так что я должен как-то сказать функцию взять переменную из InsuranceType класса, но я не знаю, как это сделать.

ответ

1

Для поля many2one мы должны сначала взять идентификатор этой записи, а затем просмотреть эту запись с идентификатором и получить значение желания от этого.

Попробуйте с этим кодом:

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 
+0

большое спасибо! – ThomasS