Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using function in domain filter

I want to load subject ids in my module related to lecturer id. I use api onchange 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 domain in my field.

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

correct subject id list passing. how can I load subjects according to those id list....?

like image 887
Chamal Avatar asked Nov 09 '22 12:11

Chamal


1 Answers

If you are using onchange method to put domain, you don't need to put any domain on 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 function field that would compute id of your needed subject and then you use directly in domain.

P.S. but your code searching for ID looks really weird, don't know why you are doing it like this.

like image 190
Andrius Avatar answered Nov 14 '22 21:11

Andrius