Using a function in a domain filter

I want to load objects in my module related to the lecturer id. I am using onchange api as follows.

@api.onchange('lecturer_id') def _onchange_lecturer(self): if self.lecturer_id: sub_id =[] a = [] lecturer = self.lecturer_id.id query = """select op_subject_id from lecturer_subject_rel where op_lecturer_id='%s'""" % lecturer self.env.cr.execute(query) a = self.env.cr.fetchall() d = 0 for i in a: e = i[0] sub_id.append(e) d += 1 return [('subject_id', 'in', sub_id)] else: return None 

and the domain in my field.

 <field name="subject_id" domain=_onchange_lecturer /> 

the correct transfer of the list of object identifiers. how can i load objects according to this list of identifiers ....?

+5
source share
1 answer

If you use the onchange method to host the domain, you do not need to put any domain in a field in xml.

You can do something like this:

 @api.onchange('lecturer_id') def _onchange_lecturer(self): res = {} if self.lecturer_id: sub_id =[] a = [] lecturer = self.lecturer_id.id query = """select op_subject_id from lecturer_subject_rel where op_lecturer_id='%s'""" % lecturer self.env.cr.execute(query) a = self.env.cr.fetchall() d = 0 for i in a: e = i[0] sub_id.append(e) d += 1 res['domain] = {'subject_id': [('id', 'in', sub_id)]} else: res['domain] = {'subject_id': []} return res 

Or you can simply create a function field that will calculate the identifier of your item, and then you use it directly in the domain.

PS but your code looking for an identifier looks really strange, I don’t know why you are doing this.

+1
source

All Articles