İçereği Atla
Menü
Bu soru işaretlendi
5 Cevaplar
17674 Görünümler

Hello my Friends;

May anyone help me know how can i get the current ID of a record before it is created in database.

I have tried :

  1. to get the active_id it does not exist in the url

  2. self._origin.id

  3. self.id

Need your help my friends please.

Thanks a lot in advance.

Best regards.

Avatar
Vazgeç
En İyi Yanıt

No, you cannot have the ID of the record if it is not created yet.

I don't know why you need the ID but if you want to access the value of the other fields, you can use onchange method to get the value of the fields and do some operations.

@api.onchange('partner_id')
def onchange_partner(self):
self.partner_id
self.my_field1
self.my_field2
   # You can access the value of the fields in the onchange like this


Avatar
Vazgeç
En İyi Yanıt

self.id is returning false because it is defined only after creation. An alternative idea to get the id before saving is find the last id of the models and calculate the next one.
For ex:
search_ids = self.pool.get('your.model').search(cr, uid, [])
last_id = search_ids and max(search_ids)



It gives the last id of that model, you will get the next id by simple addition by one. And I think you can't create all rows at a time. It is one by one process(In code).
I think it may help you
Avatar
Vazgeç
En İyi Yanıt

There are two additional ways I can think of:

- use models.NewId: This doesn't give you an actual ID (integer) but depending on your code you can use it as a placeholder.

Usage:

if isinstance(self.id, models.NewId):
    # your code

(note that in this case self.id does not exist in the db yet, but you can imagine it is already "pre-ordered" in the cache)

- It helps to know that Odoo assigns IDs in a strictly linear way: The next record will always have the ID of the previously created record plus one. I guess it is a bit of a cheaty way to work with this fact & it is only useful if you are 101% sure that nobody creates or deletes records "in between". However if you decide to try this method a good approach would be:

recordset = self.env['your.model'].search([('id', '!=', False), limit=1, order="id desc")

This will give you exactly one recordset (the newest one) & accordingly exactly one ID.

previous_id = recordset.ids[0]

So the "not yet existing" ID you are looking for is: newest_id = previous_id + 1


Avatar
Vazgeç
En İyi Yanıt
    @api.model
def create(self, vals):
res = super(SdProjectOverviewDiagram, self).create(vals)
values = vals.get('values')[0][2]
location_module = self.env['sd_project_overview.location']

location_module.create({'diagram': res.id, 'value': 2})

return res

This way you can create a record on another module while you are creating this record.
Avatar
Vazgeç
En İyi Yanıt

You can try:

@api.model
def create(self, vals):
res = super(YourClassName, self).create(vals)
print('Next ID will be:', res.id)
return res

Thanks.

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
3
Kas 17
20682
1
Eyl 20
4882
2
Ara 23
14645
0
Eki 23
33
3
Eki 23
788