如何在odoo中的选择字段中添加过滤器

时间:2016-04-12 07:22:32

标签: odoo-9

我需要在odoo中的选择字段中添加一个过滤器。

roomuser = fields.Selection([('stpi', 'Belongs to Park'),('Incubation', 'Belongs to Incubation companies'),('both', 'Belongs to Park& Incubation companies')],'Room Assignment',required=True)
roomType = fields.Selection([('meeting','Meeting Room'),('discussion','Discussion Room'),('auditorium','Auditorium'),('board','Board Room')],required=True)

这里我需要根据roomuser的值过滤roomType的值。假设roomuser值只是礼堂,而且在roomType

中应该可以看到

1 个答案:

答案 0 :(得分:1)

我已经做出如下评论,请您在下面找到它,这可能有助于您的情况:

class HotelManagement(models.Model):

    _name='hotel.management'
@api.model      
def _get_room_type_list(self):
    # [('meeting','Meeting Room'),('discussion','Discussion Room'),('auditorium','Auditorium'),('board','Board Room')]
    vals=[]
    for record in self.env['hotel.management'].search([]):
        if record.roomuser in ['stpi','Incubation']  :
            vals.extend([('meeting','Meeting Room'),('discussion','Discussion Room')])
        if record.roomuser in ['both'] :
            vals.extend([('auditorium','Auditorium'),('board','Board Room')])
    return vals


def _get_roomuser_list(self):
    return [('stpi', 'Belongs to Park'),
                            ('Incubation', 'Belongs to Incubation companies'),
                            ('both', 'Belongs to Park& Incubation companies')]


roomType=fields.Selection(string="Room Type", selection=_get_room_type_list, default='meeting', required=True)
roomuser = fields.Selection(string="Room Assignment",selection=_get_roomuser_list ,required=True)

这里我刚刚将@ api.model放在顶部_get_room_type_list上,遍历此(hotel.management)模型中的所有记录并过滤选择字段。