简体   繁体   English

如何在odoo @api.onchange 中获取选择字段的对象值

[英]how to get object value of selection field in odoo @api.onchange

i've a selection field with One2many relation i would like to modify another field upon the result of selection field but it returns object!!!我有一个具有 One2many 关系的选择字段,我想根据选择字段的结果修改另一个字段,但它返回对象!!! anybody knows how to work it任何人都知道如何工作

 from odoo import models, fields, api

 class module1(models.Model):
   _name = 'module1_module1'
   _rec_name="sem"

   sem=fields.Selection([
    ('1', '1st Sem'),
    ('2', '2nd Sem'),
    ('3', '3rd Sem')
],"Semester")
  sem_id = fields.Char("Semester ID")
  sub = fields.One2many('module1_module1_1','sem',"Subjects")




  class module1_1(models.Model):
     _name = 'module1_module1_1'


     sem = fields.Many2one('module1_module1',"semester")
     sem_id = fields.Many2one('module1_module1',"ID")

     @api.onchange('sem')
     def _ValueCount(self):
        print(self.sem.sem)

Printing the selection field returns module1_module1(,) i need specific field name like option 1 or 2 to condition accordingly打印选择字段返回 module1_module1(,) 我需要特定的字段名称,如选项 1 或 2 以进行相应的条件

Here you need to take care or avoid numeric key in selection field declaration.在这里,您需要注意或避免选择字段声明中的数字键。

   sem = fields.Selection([
       ('first', '1st Sem'),
       ('second', '2nd Sem'),
       ('third', '3rd Sem')
   ], "Semester")

Here is onchange method:这是 onchange 方法:

 @api.onchange('sem')
 def _ValueCount(self):
    # execute your logic base on semester
    if self.sem:
        sem = self.sem.sem
        if sem == 'first':
            #execute first semester logic
            ...
        elif sem == 'second':
            #execute second semester logic
            ...
        else:
            #execute third semester logic
            ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM