如何在Tryton中填写选择字段

1 投票
1 回答
965 浏览
提问于 2025-04-18 15:38

我在Tryton里有两个模块,第一个是employee(员工),第二个是pointage(考勤)。我想添加一个选择字段,这样我就可以选择pointage

为了做到这一点,我们需要创建一个元组列表,叫做pointagedef = [('', '')]。现在我们需要填充这个列表,但我找不到任何文档来理解该怎么做。

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

我想做的事情类似于:

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

1 个回答

4

你只需要声明一个包含两个值的元组列表,像这样:

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

第一个值是内部使用的,这个值会存储在数据库里。第二个值是显示给用户看的,默认情况下是可以翻译的。

你还可以传递一个函数名,这个函数会返回包含两个值的元组列表。例如:

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

如果你想访问单个表格,可以使用一个叫做Many2One的字段,在视图定义中加上widget="selection",这样客户端就会显示一个选择框,而不是默认的那种,且会预加载表格中的所有记录到这个选择框里。

撰写回答