I want to create selection drop down from the data from a model. I can't use the many2one relation because the value from the selection shouldn't be only values of id, but normally they are string codes.
My model for the selections
class Selections(models.Model): _name = 'hr.selections' name = fields.Char(string='Name', required=True) usage = fields.Char(string='Description') in_use = fields.Boolean(string='Active', default=True) fields_ids = fields.One2many('hr.selections.fields', 'selection_id')
@api.model def get_selection_field(self, selection_name): selection = self.search([('name', '=', selection_name), ('in_use', '=', True)]) selection_list = list() for data in selection.fields_ids: if data.in_use: selection_list.append((data.value, data.name)) return selection_list
class SelectionsFields(models.Model): _name = 'hr.selections.fields' _order = 'value asc' name = fields.Char(string='Option', required=True) value = fields.Char(string='Value', required=True) in_use = fields.Boolean(string='Active', default=True) selection_id = fields.Many2one('hr.selections', ondelele='cascade', index=True)
And I use it in other models like
class Something(models.Model):
_name = 'someting'
selection_field = fields.Selection(lambda self: self._get_selection('selection_name'))
def _get_selection(self, selections_name):
selections_list = self.env['hr.selections'].get_selection_field(selections_name) return selections_list
It gets the data and shows the selection options, but if you add a new record to SelectionsFields, you have to upgrade the module before it's shown. Is there a way to do this without module upgrade every time?
And NO I can't use many2one list widget for this, because its should be an addon module that doesn't change the whole program.
A shorter version of usage... does everything in the field declaration
selection_field= fields.Selection(selection=lambda self: self.env['hr.selections'].get_selection_field('selection_name'))