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

My problem is that I have to calculate the value of one field on the input of the other and vice versa.

The scenario: I've got a stored value and the first field is for percent, the second field is the stored value increased by the percent.

So if the user enters the percent it calculates the value and if he enters the value it calculates the percent.

Can this be done with the depends methods and how without getting into an infinite loop? 

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

Hi,

You can use compute and inverse method to achieve this.

Example:

percentage = fields.Float()
percentage_inc = fields.Float(compute='_compute_percentage_inc', inverse='_inverse_percentage_inc', store=True)

@api.depends('percentage')
def _compute_percentage_inc(self):
for rec in self:
value = 500
rec.percentage_inc = (value * rec.percentage) / 100

def _inverse_percentage_inc(self):
for rec in self:
value = 500
        rec.percentage = (rec.percentage_inc / value) * 100

Consider value as your stored value on which percentage computed
Inverse function will execute on saving the record

Regards

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

yeah... it kinda works, but it only triggers the inverse method when it's saved and not like with the depends decorator. That now they need even a 3rd field and input on one has to change values on other two fields.

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 6 21
4466
0
thg 4 21
2005
1
thg 4 21
2677
1
thg 3 15
4348
1
thg 4 24
1304