تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
5 الردود
17689 أدوات العرض

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.

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
3
نوفمبر 17
20694
1
سبتمبر 20
4885
2
ديسمبر 23
14673
0
أكتوبر 23
33
3
أكتوبر 23
788