Odoo Help

Welcome!

This community 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.

0

Domain on Many2Many table not working

By
Wilbert Tan
on 3/2/16, 10:07 PM 772 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',standard_id.subject_ids)]"

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

    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..

    domain="[('id','in',[1,2,3,4])]"

The error is : 

TypeError: context is undefined

http://localhost:8088/web/static/src/js/view_list_editable.js:120
1

Axel Mendoza

--Axel Mendoza--
10238
| 7 7 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 an 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

@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])]}}

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
1
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 :

@api.multi

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])]"/>



0

Jignesh Mehta

--Jignesh Mehta--
2087
| 6 4 8
Surat, India
--Jignesh Mehta--


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

Hello Wilbert,


Try below code.


@api.onchange('standard_id')

def standard_id_onchange(self):

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


Hope it works for you.

Thanks,


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

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

Your Answer

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

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/2/16, 10:07 PM
Seen: 772 times
Last updated: 3/7/16, 12:16 AM