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.
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
About This Community
|Asked: 1/22/15, 8:55 PM|
|Seen: 2725 times|
|Last updated: 3/16/15, 8:10 AM|