Search code examples
odoobackend

(Odoo) Store field names into a selection field of a separate model


How should I get all of the field names from the product.template model and store them in a selection field in a separate model?

Example

Thanks


Solution

  • I manage to fix the issue with the code below, I tried using self in the method get_fields but the following KeyError: product.template occurred so too bypass that I used request instead of self.

    The selection field consists of [(product_template_field_name_1, product_template_field_name_string_1),..,(product_template_field_name_n, product_template_field_name_string_n)], n being the number of fields in the product.template model.

    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)