(Odoo) 将字段名称存储到单独模型的选择字段中

问题描述

我应该如何从 product.template 模型中获取所有字段名称并将它们存储在单独模型的选择字段中?

Example

谢谢

解决方法

我设法用下面的代码解决了这个问题,我尝试在 get_fields 方法中使用 self,但是下面的 KeyError: product.template 发生得太绕过了,我使用 request 而不是 self。

选择字段由 [(product_template_field_name_1,product_template_field_name_string_1),..,(product_template_field_name_n,product_template_field_name_string_n)] 组成,n 是 product.template 模型中的字段数。

from odoo.http import request

def get_fields(self):
    fields = [(field,request.env['product.template']._fields[field].string) for field in
              request.env["product.template"]._fields]

    return fields

field_name = fields.Selection(
    selection=lambda self: self.get_fields(),required=True)