Counting Rows of one2many field odoo

259 views Asked by At

How can I count the rows of a one2many field and output the current row count ?

This is how it should work.For example:

1 ------column1------------

2 ------column2------------

3 ------column3------------

4 ------Column4------------

etc..

I have made an automated action for this, but it does not work as intended:

The automated action refers to the model of the one2many field and is triggered when a record is created. The following Python code is executed:

for line in record.picking_id.move_line_ids_without_package:
  for rec in str(record.x_studio_position):
    record['x_studio_position'] = len(record.picking_id.move_line_ids_without_package) 

What happens is the following for e.g. 4 columns

4 ------column1------------

4 ------column2------------

4------column3------------

4 ------column4------------

It will write the total number in each row instead of the current column number.

1

There are 1 answers

0
Kenly On BEST ANSWER

You set the position to the number of lines in the field move_line_ids_without_package, it will be the same for all lines.

You can use enumerate to get the line sequence

Example:

for index, line in enumerate(record.picking_id.move_line_ids_without_package):
    line['x_studio_position'] = index + 1