Help

2

[Solved] [odoo11] : How to get database ID of external IDs using python external script?

Hi All,

Is there any way, we could find the Actual database IDs of records if we have external IDs, using python script.

With XMLRPC or erppeek lib?

For. Eg.

I have 1 table inside odoo with test_test name. Imported some records inside the table using demo data of csv with external ID.

test_001, test_002, so on.

How we could access the data using python script from the table based on these external IDs?

Any idea appreciated.

Rgds,

Anil.

Avatar
Discard
4 Answers
2

Hi Anil,

You can call search method using XML-RPC:

rpc_obj.execute(db_name, uid, pwd, 'ir.model.data', 'search', [('model', '=', 'res.partner'), ('name', '=', ext_id)])

Or you can create your own method in custom module and can call it using XML-RPC.

Hope this will help you.

Sudhir Arya
ERP Harbor Consulting Services
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com
5 Comments
Avatar
Discard

Thanks sudhir, found solution to get data of csv Id, any idea how to fetch database ID of imported record through xml ID?

Do you want DB ID using XML-RPC?

Yes. and I have the XML ID of that record as reference.

You can use same code I posted in my answer. When you record of ir.model.data object, get the value of "res_id" field.

Solved sudhir, thanks for your concerned and answer. :)

1
Best Answer

Hi All,

I have found the solution using ERPPeek lib. which using xmlrpc lib itself.

1. Here is the example, by using I found out Database ID of csv ID.

import erppeek
client = erppeek.Client('http://localhost:8888', 'database_name', 'username', 'pass')
model_data = client.model('ir.model.data')
source_data = model_data.search_read([('model','=','model_name'), ('name','=',external_csv_id)], ['res_id'])

The source data returns database ID as res_id.

This works perfectly if data loaded through csv file.

2. Here is the method Which I have tried to search database ID of record which have been imported  using XML IDs.

import erppeek
client = erppeek.Client('http://localhost:8888', 'database_name', 'username', 'pass')
 model_data = client.model('ir.model.data')
result = model_data.xmlid_lookup("%s.%s" % ('module_name', xml_id))
It returns the list of 3 items.
1 values is backend external ID
2. Model Name
3. Database ID.
o/p E.g. : [2172, 'res.groups', 3]

This is How, I found solution to get the database ID of the records with reference of XML ID.

works like a charm.

Regards,

Anil.


Avatar
Discard
1
Best Answer

Hello

I hope I serve you here I have an example

Example 01:

self.env['ir.model.data'].search_read([('model', '=', 'res.partner'), ('name', '=', 'external_id_csv')], ['res_id'])

return tuple

# [{'res_id': id_database, 'id': id_record_model}]

example 02:

self.env['ir.model.data'].xmlid_to_res_id('your_module.external_id_csv')

1 Comment
Avatar
Discard

self.env is accessible outside odoo.

0
Avatar
Florent
Best Answer

There's a simpler solution involving ERPpeek :


----
# Let's say your Odoo module is 'test_module'
# and your csv contains external_ids 'test_001' 'test_002' for "res.partner" records

partner_001 = model("res.partner").get('test_module.test_001')
partner_002 = model("res.partner").get('test_module.test_002')

print(partner_001.id)
print(partner_002.id)


----
Avatar
Discard