Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
12241 Lượt xem

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. 



Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả Câu trả lời hay nhất

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. 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, 

You can follow following link for this:

https://youtu.be/GPhgxxwprA4

Hope it helps,

Thanks

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
5
thg 5 22
25358
0
thg 2 18
4815
3
thg 6 19
6404
1
thg 7 17
4825
2
thg 9 23
8735