跳至内容
菜单
此问题已终结
10 回复
15287 查看

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.

形象
丢弃
最佳答案

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
形象
丢弃
编写者

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. :)

编写者 最佳答案

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.


形象
丢弃
最佳答案

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')

形象
丢弃
编写者

self.env is accessible outside odoo.

最佳答案

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)


----
形象
丢弃
相关帖文 回复 查看 活动
3
1月 19
6715
2
7月 24
13777
1
7月 21
2455
1
7月 21
7932
0
4月 20
4563