This question has been flagged
1 Reply
3537 Views

I have a small dout .I have a many2one field like "course"(btech,metch, btech have 8semesters and mtech have 4semsters) another field is also many2one like "semester" when i select btech in course to automatically change the value in semester field by using compute method how i can do please suggest me.

Avatar
Discard
Best Answer

write an onchange function on the field course.

eg:

@api.onchange('course')
def onchnage_course(self):
""""""
if self.course.name == 'btech':
# x should be the current semester
self.semester = x #OR RETURN AS A DICT
        return {'values': {'field_name': value}}

or you can make the semester field as compute field. simply by giving the attribute compute='function name' and return the result. don't forget to give the course field name in depends.

eg:

@api.depends('course')
def func(self):
""""""
Avatar
Discard
Author

okay hilar how can we pass the return value into the field as a list

return as a dict{}. the answer is updated

Author

@api.depends('course_id')

def _compute_set_sem(self):

sem_list=[]

if self.course_id:

if self.course_id =='btech':

count = 0

for bsem in range(1,9):

count += 1

year = bsem

if bsem % 2 == 0:

sem = (str(year/2)+ "-2")

else:

if bsem > 2:

sem = (str(year/2 + 1)+ "-1")

sem = (str(year/2 + 1)+ "-1")

tuple_sem = (str(bsem),sem)

# print tuple_sem

sem_list.append(tuple_sem)

# print sem_list

return {'values': {'btech_sems': sem_list}}

btech_sems=fields.Selection(compute='_compute_set_sem',store = True, string = 'Semester')

i wrote like this hilar but displyed assertion error

AssertionError: Field regular.single.btech_sems without selection

here you are adding a list of values to a single field. What you need actually? Domaining with the result? Compute function compute a value for the field in the current view. If you need to give domain then return dict by replacing values as 'domain', but not in a compute method but in a onchange function.

Author

yes exacty i want to add list of values to a single field (selection field)

try fields.Selection(selection=method_name_which_returns_list)