Search code examples
pythontryton

how fill Selection field in tryton


I have 2 modules in tryton the first is employee and the second is pointage. I am trying t add a Selection field that allow me to select the pointage.

To do that we have to create a list of tuple pointagedef = [('', '')] now we have fill it but the problem that i can't find any documentation to understand how to do it

pointage= fields.Selection(pointagedef, 'grh.pointage')   

i am trying to do something like:

for pointage in pointages:
    pointagedef.append((pointage, pointage))

Solution

  • You just have to declare a list of two value tuples with the values. Something like:

    colors = fields.Selection([
       ('red', 'Red'),
       ('green', 'Green'),
       ('blue', 'Blue'),
    ], 'Colors')
    

    The first value will be the internal one, and that will be stored on the database. The second value is the value shown on the client and by default is translatable.

    You can also pass a function name, that returns the list of two value tupple. For example:

    colors = fields.Selection('get_colors', 'Colors')
    
    @classmethod
    def get_colors(cls):
       #You can access the pool here. 
       User = Pool.get('res.user')
       users = User.search([])
       ret = []
       for user in users:
          if user.email:
             ret.append(user.email, user.name)
       return ret
    

    Also if you want to access a single table, you can use a Many2One field, adding widget="selection" on view definition, so the client will render a selection widget instead of the default one, and preload all the records of the table to the selection.