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.
На данный момент мой код приводит к
'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