Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
11626 Vistas

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. 



Avatar
Descartar
Autor

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'))

Autor Mejor respuesta

It works. The options of the selection are stored in the browser cache so after closing the browser or a ctrl + F5 it shows the correct options. 

Sorry for posting useless questions that are no problems at all. 

Avatar
Descartar
Mejor respuesta

Hi, 

You can follow following link for this:

https://youtu.be/GPhgxxwprA4

Hope it helps,

Thanks

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
5
may 22
24788
0
feb 18
4366
3
jun 19
5838
1
jul 17
4432
2
sept 23
8162