Dynamic Selection field

(Prakhar) #1

I am trying to create a dynamic selection field here. The idea can be explained with the help of given code example -

Model A - 
name = fields.Char('Name')
option_yes = fields.Char('If it is okay')
option_no = fields.Char('If it is not okay')

Model B - 
field_a = fields.Many2One(Model A, 'A')
state = fields.Selection([
    ('yes', <Value in option_yes>)
    ('no', <Value in option_no>)


I tried writing something like this for field ‘state’:

def get_selection(self):
    list_ = [('', '')]
    if self.field_a:
        list_ = [
            ('', ''),
            ('y', self.field_a.option_y),
            ('n', self.field_a.option_n),
    return list_

and changed the field definition to -

state = fields.Selection(‘get_selection’, ‘State’, depends=[‘field_a’])

I am getting the following error on client-

ERROR:tryton.common.common: File “/home/prakhar/tryton/init.py”, line 49, in callback
return func(*args)
File “/home/prakhar/tryton/gui/window/view_form/view/list_gtk/widget.py”, line 57, in wrapper
return func(self, *args, **kwargs)
File “/home/prakhar/tryton/gui/window/view_form/view/list_gtk/widget.py”, line 102, in wrapper
func(self, column, cell, store, iter)
File “/home/prakhar/tryton/gui/window/view_form/view/list_gtk/widget.py”, line 200, in setter
text = self.get_textual_value(record)
File “/home/prakhar/tryton/gui/window/view_form/view/list_gtk/widget.py”, line 812, in get_textual_value
self.update_selection(record, field)
File “/home/prakhar/tryton/common/selection.py”, line 57, in update_selection
File “/home/prakhar/tryton/common/selection.py”, line 44, in init_selection

‘<’ not supported between instances of ‘NoneType’ and ‘str’

Can someone tell me what I am doing wrong here? And what will be the better way to do it.

(Cédric Krier) #2

It looks like some of your second item of listed couples are None which is not allowed (only strings).