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

问题描述 投票:0回答:1

我知道我的对象类型不匹配以进行比较。我尝试了@ api.onchange,但它说:

NullObject不可用于选择字段。

代码:

 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')]
python erp odoo-12
1个回答
1
投票

您可以将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"/>
© www.soinside.com 2019 - 2024. All rights reserved.