Odoo Help


Domain on Many2Many table not working

Wilbert Tan
on 3/2/16, 10:07 PM 1,844 views

Hi Guys, 
I know there is a lot of question regarding this, but I can't make it to work on my case.

I have field selected_subject_ids under student_registration object, 

in the student_registration object i have field standard_id and inside of it i have fields many2many subject_ids.

Basically what I want to do is to filter the selected_subject_ids to only select subject under the standard_id.subject_ids

What I've tried so far in the selected_subject_ids field:



    domain="[('id','in',[i.id for i in standard_id.subject_ids])]"

I also try to change the operator to '=', same case...

But when I try to hard code it, it works..


The error is : 

TypeError: context is undefined


Axel Mendoza

--Axel Mendoza--
| 7 8 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full-time employee trying to understand what to do and how

Reach me at aekroft@gmail.com

Axel Mendoza
On 3/3/16, 1:04 AM

My solution is a mix with what @Jignesh Mehta is proposing, you could do it like this:

def _get_subject_domain(self, cr, uid, ids, name, arg, context=None):

res = {}

for sub_id in self.browse(cr, uid, ids, context=context):

res[sub_id.id] = [(6, 0, [subj.id for sub_id.standard_id.subject_ids])]

return res

_columns = {


'subject_domain_ids': fields.function(_get_subject_domain, type='many2many', relation='subject.model_name', string='Subject Domain'),



in the view

<field name="subject_domain_ids" invisible="1"/>

<field name="standard_id"/>

<field name="selected_subject_ids" domain="[('id','in', subject_domain_ids[0][2])]"/>

and the modified @Jignesh Mehta onchange returning the domain for the field selected_subject_ids

def standard_id_onchange(self):
    return {'domain': {'selected_subject_ids': [('id', 'in', [subj.id for subj in self.standard_id.subject_ids])]}}

Hi Axel, Thanks for your reply. Actually I've been thinking to do this way, But what if the student registration already has the standard_id filled before. Then the onchange won't work. Domain works on onchange, but why it won't work on field based? This code doens't work: selected_subject_ids = fields.Many2many('subject.subject', 'reg_subject_rel', 'reg_id', 'subject_id', 'Selected Subjects', domain="[('id', 'in', [subj.id for self.standard_id.subject_ids])]") And by the way, you have some typo over here. I've modified it to work like this: @api.onchange('standard_id') def standard_id_onchange(self): return {'domain': {'selected_subject_ids': [('id', 'in', [subj.id for subj in self.standard_id.subject_ids])]}}

Wilbert Tan
on 3/3/16, 1:39 AM

Yes, I see that I miss subj var in the list compression. I just try to fix the code of @Jignesh Mehta to see if using it you could solve your problem. Let me give you the way I use to do it. I will override my answer with the new one

Axel Mendoza
on 3/3/16, 2:05 AM

It was a mix finally what I'm using

Axel Mendoza
on 3/3/16, 2:22 AM

You could also set the initial value for the form to use a domain value from where the form is not saved yet and the function does not get calculated already.

Axel Mendoza
on 3/3/16, 2:25 AM

Hi axel, I manage to make it work as per your code. Will post the updated code in the new api. Btw, what do you mean on the initial value so it doesn't calculate ? Thank you, axel

Wilbert Tan
on 3/7/16, 12:13 AM

Btw Axel, don't you think that kind kind of method will make the server become slow ? (as function will always load )

Wilbert Tan
on 3/7/16, 12:17 AM

I mean that function or computed field only will have their values calculated after they exist in the database. So for a new record you need to set it's value using a default or with onchanges, Otherwise the value could not be taken into account. For your second question the answer is no, I don't thinks that, it will no make the server slow

Axel Mendoza
on 3/7/16, 4:19 AM
Wilbert Tan
On 3/7/16, 12:15 AM

Hi all, 

I manage to make it work like Axel's solution.

Here is the code in the api v8:

add this field :


def _get_subject_domain(self):

    for record in self:

        record.subject_domain_ids = [(6, 0, [subj.id for subj in record.standard_id.subject_ids])]

subject_domain_ids = fields.Many2many('subject.subject', string='Subject Domain', compute='_get_subject_domain')

on the view : 

<field name="subject_domain_ids" invisible="1"/>

<field name="selected_subject_ids" domain="[('id','in', subject_domain_ids[0][2])]"/>


Jignesh Mehta

--Jignesh Mehta--
| 6 5 8
Surat, India
--Jignesh Mehta--

- I have 3+ years of Industry Experience, specializing Odoo Development, Analysis, Customization, Implementation, Integration.
- I have done many Customization modules in Sale, Purchase, Point of Sale, Inventory, CRM and much more. Also Worked in the Odoo Themes like Business Theme or E-Commerce Theme.
- Develop Jewelery Manufacturing process from Scratch. It have more than 10 Department. Develop PDF and Excel reports for this project around 150 per format.

Jignesh Mehta
On 3/2/16, 11:12 PM

Hello Wilbert,

Try below code.


def standard_id_onchange(self):

    return {'domain': {'subject_ids': [('standard_id', '=', self.standard_id.id)]}}

Hope it works for you.


Hi Jignesh. Thanks for your reply. Please see my commend of other post above. thanks

Wilbert Tan
on 3/3/16, 1:41 AM

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: 3/2/16, 10:07 PM
Seen: 1844 times
Last updated: 3/7/16, 12:16 AM