Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
5 Відповіді
17718 Переглядів

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.

Аватар
Відмінити
Найкраща відповідь

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


Аватар
Відмінити
Найкраща відповідь

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
Аватар
Відмінити
Найкраща відповідь

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


Аватар
Відмінити
Найкраща відповідь
    @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.
Аватар
Відмінити
Найкраща відповідь

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.

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
3
лист. 17
20720
1
вер. 20
4907
Odoo Mail Sending Limit Вирішено
2
груд. 23
14721
0
жовт. 23
33
3
жовт. 23
788