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

Hello!

I have a table like this:

row_numbersome_data_1some_data_2
1

2


And I want the row_number to be automatically set to values ​​starting from 1


How can I do it?

What code can I write in "Advanced Properties - Compute" on the row_number field?


I tried to do like this:

increment = 1
for record in self:
record['row_number'] = increment
increment = increment + 1

But when trying to add new lines - new lines start at 1 again


Help, please!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
Using Odoo Sequence you can implement your idea 
firs create the sequence using xml code
then in py file call you sequence like following cod:

@api.model
def create(self, vals):
vals['row_number'] = self.env['model.model'].next_by_code('cod.cod')
return super(ClassName, self).create(vals)


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

Hi!

Why don't you just retrieve the id of the record :)? When a new record is created, Odoo will automatically give it as an ID the next number in the list (so that it's always unique). You can retrieve it with record.id.

You may also have a look at the Getting Started tutorial in Odoo Docs if you haven't already ;) :https://www.odoo.com/documentation/15.0/developer/howtos/rdtraining/04_basicmodel.html

Hope this helps!

Best,

Fabio

Ảnh đại diện
Huỷ bỏ
Tác giả

I had an attempt to do it with code like this, however it throws an error because record.id is not "int" at this point:
id_on_start = record.id
for record in self:
record ['row_number'] = int (record.id) - int (id_on_start) + 1

If you know the code with which I was able to get the id of the created record of type "int" I will be very grateful. Or if you can suggest another way - I will be very happy

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 15
4310
2
thg 11 22
7187
2
thg 12 23
4821
3
thg 2 17
6779
1
thg 7 25
2328