Skip to Content
Menu
This question has been flagged
1 Reply
1651 Views

class mk_link_student(models.Model):
    _name = 'mk.link.student'
    _description = 'Link Student With Stage'
    _rec_name = 'name_stage_id'

    name_stage_id = fields.Many2one(
        'mk.stage',string='Name Stage',
    )

    remain_num = fields.Integer(
        string='Remaining number',
        compute='total_student'
       
student_ids = fields.Many2many('mk.student.register', string = 'Student')

i want compute field in remain_number depend on student_ids field many2many

@api.one
    @api.depends('student_ids')
    def total_student(self):
        max_n = self.name_stage_id.max_number
        for rec in self:
            student = rec.student_ids.ids
            if student:
                result = max_n - student

and give me this Error:

result = max_n - student TypeError: unsupported operand type(s) for -: 'int' and 'list'

Avatar
Discard
Best Answer

Hi Sari,

In the student currently you will get a list like this [1,2,3] , after this operation ->   student = rec.student_ids.ids .
This list is you are trying to subtract from the max_n , thats why you are getting this error.

You can use a for loop to iterate over the student_ids, like this and you can get the value from the field,

for student_rec in student_ids:
     test_var = student_rec.field_name

later this value in the test_var can be assigned to result or subtract from the max_n to get the result.

I don't know what exactly you are looking for.

Thanks


Avatar
Discard
Author

thanks i solved my problem