This question has been flagged
9 Replies
7058 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
Avatar
Discard
Best Answer

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])]}}
Avatar
Discard
Author

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

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

It was a mix finally what I'm using

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.

Author

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

Author

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

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

Author Best Answer

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



Avatar
Discard
Best Answer

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,


Avatar
Discard
Author

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