This question has been flagged

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>


Avatar
Discard
Author Best Answer

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
Avatar
Discard
Author

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