Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
9421 Lượt xem

Hello Everyone,

Can someone explain me what's wrong with my code ?

i'm getting the following error:

UnboundLocalError: local variable 'total_amount' referenced before assignment

My Code:

payment_id = fields.Integer(string='Payment ID')
    ammount = fields.Integer ("Value 1")
    rate = fields.Integer ("Value 1")
    total = fields.Float (string = 'Multiplicar'compute = '_compute_total')

    @api.depends ('ammount''rate','payment_id')
    def _compute_total (self):
        for record in self:
            if str(record.payment_id) == 1:
                record.total_amount = record.ammount * record.rate
            elif str(record.payment_id) == 2:
                total_amount = record.ammount / record.rate
            else:
                self.total = total_amount

My request is to calculate TOTAL (multiply or divide) depending on the Payment(Currency) ID.

Ex:
Currency1: USD (ID:1)
Currency2: LBP (ID:2)
if Currency1 selected then ammount is Multiplied by the rate
if Currency2 selected then ammount is Divided by the rate
everything else than these Currencies is equal to the main ammount.

Hope someone can solve it.

Cheers

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, 

You are getting this error because you haven't assigned what value should total_amount hold , You need to define the variable before using it .

    def _compute_total (self):
        for record in self:
            if str(record.payment_id) == 1:
                record.total_amount = record.ammount * record.rate
            elif str(record.payment_id) == 2:
                total_amount = record.ammount / record.rate
            else:
                self.total = total_amount  // Error is in this line , because you need to define total_amount before assigning .
 

 


Ảnh đại diện
Huỷ bỏ
Tác giả

Hello Kiran,

Thank you a lot for your raply.

You mean by your answer defining total_ammount as a field ? if not then how to define it in this situation ?

Thank you again.

Tác giả Câu trả lời hay nhất
FIX based on Kiran Mohan's reply:

@api.depends('ammount''rate','payment_id')
 def _compute_total(self):
  for record in self:
   if int(record.payment_id) == 1:
    record.total = record.ammount * record.rate
   elif int(record.payment_id) == 2:
    record.total = record.ammount / record.rate 
   else:
    record.total = record.ammount
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 1 24
1418
1
thg 11 23
10041
5
thg 11 22
3885
1
thg 5 22
4606
0
thg 3 22
2996