Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
12373 Näkymät

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
Hylkää
Tekijä

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

Tekijä Paras vastaus

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
Hylkää
Paras vastaus

Hi, 

You can follow following link for this:

https://youtu.be/GPhgxxwprA4

Hope it helps,

Thanks

Avatar
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
5
toukok. 22
25480
0
helmik. 18
4889
3
kesäk. 19
6499
1
heinäk. 17
4886
2
syysk. 23
8802