This question has been flagged
1 Reply
3542 Views

Hello All,

i Have a field, in which total weight shows of all rows. And want that if total "Sum" is greater than 50 then it shows error that "Weight Exceeded". My function working up-to "if self.sum >= 50: "

My Code is here:

@api.onchange('machine_weight_id', 'weight')

    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

                    self.sum = vals

                    if self.sum >= 50:

                        raise UserError(_('Weight Exceeded'))

Avatar
Discard
Best Answer

Hi Pawan,

I suppose that machine_weight_id in M2O, since you wrote it that way.

weight_total = Fields.Float(..., compute='_compute_total_weight') 

@api.multi

def get_total_weight(self):

 total= 0;

 if self.machine_weight_id:

     weight_obj = self.env['your comodel_name'].search([])

     #here it returns all records in the model

     for rec in machine_weight_id:

     #here you can access each records using rec

         if rec.weight:

             total += rec.weight

 return total

@api.multi

@api.depends('machine_weight_id', 'weight')

def _compute_total_weight(self):

for record in self:

    record.weight_total = record.get_total_weight()

    if record.weight_total >= 50.00:

          raise UserError(_('Weight Exceeded'))

UserError should be imported : from odoo.exceptions import UserError

Avatar
Discard