Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
I would like to fetch records over the API using the external ID of those records. Is there a simple way to do this that I am overlooking, because my current approach seems very convoluted.
Given complete names 'l10n_uk.state_uk_99' and 'l10n_uk.state_uk_98', I would like to fetch the two states they represent.
First I need to split the complete names into modules and names. This gives me module "l10n_uk" and names "state_uk_99" and "state_uk_98". If there are several modules involved, then that is another pain point to work around, but let's assume there is just one.
Now I need to search ir.modules.data for these external IDs using this criteria:
array("model", "=", "res.country.state"),
array("module", "=", "l10n_uk"),
array("name", "in", array("state_uk_99", "state_uk_98")),
This returns me the two IR model IDs. Then I need to fetch those IR model records - I just want the res_id field:
$my_api->read('ir.modules.data', array(5310, 5311), array('res_id'));
That gives me an array of two arrays. I need to loop through them to find and collect the res_id IDs.
Now I have those IDs, I can fetch the underlying records:
That gives me the underlying two states behind those original external IDs. BUT those records don't contain the external IDs, so I must make sure I keep the external_id:res_id lookup or distribute the external IDs over the state records I have retrieved.
What a faff! Are there any short cuts? Can I search states directly and somehow include the ir.module.data in the search criteria? When reading the states, can I include the external IDs in the retrived columns?
I realise the relationship between the states and the external IDs will be 1:1 only in context (e.g. for my locale) and may not be 1:1 at the database level, so this probably can't be done any other way. But I do need to ask...
To fetch a record by external id, try this method:
some_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, module_name, xml_id)
in your case that would be like:
some_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, l10n_uk, state_uk_99)
and you will have current id for the record..
It may be a bit late, but for future reference, i encountered the same problem and i solved it with the following:
odoo = odoorpc.ODOO(server, port=port)
odoo.login(db, username, password)
country_id = odoo.env['ir.model.data'].get_object_reference('base',country)
where "country" was "ca", "be", "us",...
About This Community
This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 7/24/14, 11:32 AM|
|Seen: 4002 times|
|Last updated: 2/8/17, 4:34 AM|