问题
I need to add a filter to a selection field in 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)
Here i need to filter the value of roomType based on the value of roomuser. Suppose roomuser value is both only auditorium and board should be visible in roomType
回答1:
I have made my comments as below , kindly request you to find it as below it may help in your case:
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)
Here i have just put @api.model on top _get_room_type_list and traversing all the record in this(hotel.management) model and filtering the selection field .
来源:https://stackoverflow.com/questions/36566281/how-to-add-a-filter-to-selection-field-in-odoo