我想修改我的选择字段,但没有输出

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

我知道我的对象类型不匹配,无法比较。我试过 @api.onchange 但它说:

NullObject is not iterable for selection fields.

代码:

 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')]

您可以在视图定义中将 Semester 字段类型更改为 many2one 并将属性 widget 设置为 selection
cou_id 的值发生变化时,您只需通过返回自定义域来过滤选择字段中显示的记录。

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)]}}

在视图定义中:

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