Search code examples
pythonerpodoo-12

I like to modify my selection field but there are no output coming


I know my object type is not matching to compare. I tried on @api.onchange but it says:

NullObject is not iterable for selection fields.

Code:

 from odoo import models,api,fields
 class semester(models.Model):
    _name = 'module2_semester'
    _description = 'Semester_Info'
    _rec_name = 'sem_id'


   sub = fields.Many2many('module2_subject')
   cou_id = fields.Many2one('module2_course',"Course Name",required=True)
   sem_id = fields.Char("Semester ID")
   sem_name = fields.Selection(selection='_getSem_value',string="Semester")
   reg_no = fields.Integer("Registration Number",size=20)

   @api.model
   def _getSem_value(self):

        print("hello")
        print(self.cou_id)
        if self.cou_id=='BTECH':

             return [('1','1'),
                     ('2','2'),
                     ('3','3'),
                     ('4','4'),
                     ('5','5'),
                     ('6','6'),
                     ('7','7'),
                     ('8','8')]
        if self.cou_id=='MCA':

           return [('1','1'),
                   ('2','2'),
                   ('3','3'),
                   ('4','4'),
                   ('5','5'),
                   ('6','6')]
      if self.cou_id=='MTECH':


           return [('1','1'),
                   ('2','2'),
                   ('3','3'),
                   ('4','4')]

Solution

  • You can change the Semester field type to many2one and set the attribute widget to selection in the view definition.
    When the value of cou_id changes you have just to filter the records shown in the selection field by returning a custom domain.

    class Semester(models.Model):
        _name = 'semester'
        _description = 'Semester'
    
        name = fields.Integer()
    
    class semester(models.Model):
        _name = 'module2_semester'
        _description = 'Semester_Info'
        _rec_name = 'sem_id'
    
    
        sub = fields.Many2many('module2_subject')
        cou_id = fields.Many2one('module2_course',"Course Name",required=True)
        sem_id = fields.Char("Semester ID")
        # sem_name = fields.Selection(selection='_getSem_value',string="Semester")
        reg_no = fields.Integer("Registration Number",size=20)
    
        sem_name = fields.Many2one('semester', string="Semester")
    
        @api.onchange('cou_id')
        def _course_changed(self):
            self.sem_name = False
            if self.cou_id:
                # Initialize `semesters` in case `cou_id.name` is not listed below
                semesters = 0
                if self.cou_id.name == 'BTECH':
                    semesters = 8
                if self.cou_id.name == 'MCA':
                    semesters = 6
                if self.cou_id.name == 'MTECH':
                    semesters = 4
    
                sem_ids = self.env['semester'].search([('name', '<=', semesters )])
    
                return {'domain': {'sem_name': [('id', 'in', sem_ids.ids)]}}
    

    In the view definition:

    <field name="sem_name" widget="selection"/>