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.
In v8 I have three models, ModelA, ModelB and ModelC.
- ModelA has a One2many field: field_b_ids relating ModelA and ModelB.
- ModelB has a Many2many field: field_c_ids relating ModelB and ModelC.
Somewhere else I have one id for one ModelA record (a_id).
my_a = self.env['model.a'].search([('id', '=', a_id)])
I get the recordset with the one ModelA record I expect.
From there, I need to get all the ids (or records in a recordset) of ModelC that are in field_c_ids in those ModelB records which are in field_b_ids in my ModelA record.
I thought using mapped() (as read in http://odoo-new-api-guide-line.readthedocs.org/en/latest/environment.html#useful-helpers ) would be my solution, like this:
my_c_ids = my_a.mapped(field_b_ids.field_c_ids)
But mapped() seems to work OK with everything except One2many and Many2many.
Also, trying to go step by step, using loops, if I try to get the ModelB records like this:
my_b_ids = my_a.field_b_ids
This fails complaining about expecting singleton, even if I do:
for a in my_a:
my_b_ids = a.field_b_ids
I tried variations on this, like using a.field_b_ids (seen for XML domains in https://www.odoo.com/forum/help-1/question/how-to-define-a-domain-for-a-field-a-depending-on-the-ids-of-other-field-b-in-a-view-73115 , but it seems to work only in XML domains, not in Python code).
So... How do I get it?
I am running completely out of options here. I feel like it should be easier for o2m, m2m and be (better) documented.
I already found the problem: I was defining a field as One2many when it should be Many2many (to work with its Many2many counterpart).
Once fixed that, everything works fine and the use of mapped() feels nice and super-easy.
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: 1/22/15, 8:55 PM|
|Seen: 5295 times|
|Last updated: 4/19/17, 3:48 AM|