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

hello,


trivial question came to me, but - shame on me - I was not able to provide simple answer; considering I have two fields in form:


<group col="2">

<field name="x_po"/><newline/>

<field name="x_sz"/><newline/>

</group>


is there a simple way to add third field, which will show sum of above? something like "x_po+x_sz"?


regards

Tomasz



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

Make the third field as compute field

in your script just compute the total of two fields and return the value

if you need to display the total sum you just need to give sum="thirdfield" in your tree view

 @api.one
 def _get_total(self):
try:
         self.thirdfield = self.first + self.second
        except:
             raise "here add your exceptions"

 thirdfield = fields.Float(compute='_get_total', string="")
Ảnh đại diện
Huỷ bỏ

don't forget to close try

Have an upvote

Câu trả lời hay nhất

create a new field x_po_plus_x_sz



@api.onchange('x_po','x_sz')   

def getsum_x_po_x_sz(self):
        if self.x_po and self.x_sz:
            self.x_po_plus_x_sz=self.x_po + self.x_sz

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

You need to make a compute field for the third one with an onchange annotationthirdfield = fields.Float(compute='_compute_sum', string="SUM")

@api.onchange('x_po','x_sz')

def _compute_sum(self): 

        self.thirdfield = self.x_po + self.x_sz

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

Hi Tomasz

You need to add a calculated field that compute the calculation and if you wanna make it more dynamically you could add also an onchange for the source field for the sum to provide the result without the need of saving the form

Read more about it in the docs

https://www.odoo.com/documentation/10.0/reference/orm.html#fields

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

hello,

thank you for answers; I had hope it is to be done inside form view - but I guess code as above is to be added somewhere else, am I right? if yes - sorry for asking - where to insert code as above:

---

@api.one

def _get_total(self):

try:

self.thirdfield = self.first + self.second

thirdfield = fields.Float(compute='_get_total', string="")

---

regards

Tomasz

Ảnh đại diện
Huỷ bỏ

In your model, ie where you defined those fields or by inheriting the parent model,use the function

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 9 23
2227
0
thg 2 25
1317
0
thg 1 25
1138
1
thg 12 24
1467
1
thg 6 22
5609