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

Hello All,

I have a One2many field and three fields inside the One2many field 'name','weight' and 'price'. I want that all values for 'weight' field added from multiple rows of One2many field.

My Python and XML Code is here:

Python Code:

class body_processing(models.Model):
_name = 'body.processing'
     
     machine_weight_id = fields.One2many('machine.weight', 'machine_id', string='Weight         Machine')
class machine_weight(models.Model):
_name = 'machine.weight'

@api.depends('weight','sum', 'machine_id')
def _onchange_amount_weight(self):
sum = 0.0
vals = 0.0
for line in self.machine_id:
vals = line.weight + line.sum
print"a", vals
line.sum = line.vals
print"b", line.sum
line.sum += line.weight
print"c", line.sum

machine_id = fields.Many2one('sale.pet', string='Machine Weight', ondelete='cascade', index=True,
copy=False)
pet = fields.Char()
weight = fields.Integer()
price = fields.Integer()
sum = fields.Integer('SUM', change_default = True, default= _onchange_amount_weight)

XML Code:

<group>
<field name="machine_weight_id" style="width:630px">

<tree editable="bottom">
<field name="pet"/>
<field name="weight"/>
<field name="price"/>
<field name="sum"/>
</tree>
</field>
</group>


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

For this problem i applied another function and i reached to print value of weight for every field. But how to access value weight from every row and add that values?

My Function is here:

@api.onchange('machine_weight_id')
    def _onchange_amount_weight(self):
        weight_obj = self.machine_weight_id
        print "a:", weight_obj
        for vals in weight_obj:
            print"v::", vals
            print "s", vals.weight
            print'sum', vals.weight
Ảnh đại diện
Huỷ bỏ
Tác giả

I donw this:

@api.onchange('machine_weight_id')

def _onchange_amount_weight(self):

if self.machine_weight_id:

weight_obj = self.machine_weight_id

print "a:", weight_obj

vals = 0;

for rec in weight_obj:

print"v::", rec

if rec.weight:

print "s", rec.weight

vals = vals + rec.weight

print "vals", vals

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 22
8049
Set Default Country Đã xử lý
2
thg 6 20
13733
0
thg 7 19
4211
0
thg 6 18
2555
0
thg 9 17
3485