Odoo Help


How to get a recordset from a One2many or Many2many field in a python file?

Alejandro Santana
on 1/22/15, 8:55 PM 5,777 views

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[0][2] (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.






| 5 3 6
Jakarta, Indonesia
On 1/22/15, 11:04 PM

my_a is a list of integers (database IDs), you should do self.env['model.a'].browse(my_a) first to get the recordset.  After which you should be able to chain it:

for a in self.env['model.a'].browse(my_a):

    my_b_ids = a.field_b_ids


But in v8 new API search() returns also a recordset, not a list of ids anymore. (See https://www.odoo.com/documentation/8.0/reference/orm.html#common-orm-methods )

Anubía, soluciones en la nube, S.L., Alejandro Santana
on 1/23/15, 9:00 AM

My apology, you're right. Reading further to the link that you have shared in your question. The example stated that you need to supply a string to mapped. Your code does not seems to supply a string. Maybe that is the problem?

on 1/24/15, 12:55 AM

I found my problem already. A simple stupid one: I defined a field as One2many, when it should be Many2many to be correct. With that fix, mapped works like a charm! (I will edit my question to state this)

Anubía, soluciones en la nube, S.L., Alejandro Santana
on 1/26/15, 1:53 PM

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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 1/22/15, 8:55 PM
Seen: 5777 times
Last updated: 4/19/17, 3:48 AM