Skip to Content
Menu
This question has been flagged
1 Reply
2097 Views

Hi. I want to get line numbers in the purchase order. But it raises error regarding NewID when lines got changed. How it can be implemented ?

Doing it like this:

  

class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'

line_number = fields.Integer(string='#', default=1)


class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

@api.onchange('order_line')
def _compute_line_number(self):
sorted_lines = sorted(self.order_line, key=lambda d: (d['sequence'], d['id']), reverse=False)
cnt = 1
for l in sorted_lines:
l.line_number = cnt
cnt += 1
Avatar
Discard
Best Answer

Hey try this piece of code

def _get_line_numbers(self):
        line_num = 1   
        if self.ids:
            first_line_rec = self.browse(self.ids[0])

            for line_rec in first_line_rec.order_id.order_line:
                line_rec.line_no = line_num
                line_num += 1


    line_no = fields.Integer(compute='_get_line_numbers', string='Serial Number',readonly=False, default=False)

Avatar
Discard