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....?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With