Add value of a selection field on report

I have the next field:

cost_method = fields.Selection([
        ('fixed', 'Fixed Price'),
        ('calculation', 'Based on Work Effort'),
        ('warranty', 'Warranty'),
        ('noinvoice', 'No invoicing')
    ], 'Cost Method')

I also want that field on the report, so I added <record.cost_method> to the report. It works, but I got the first entry from the tuple (fixed, calculation, …) instead of the second one. So I created a small function and moved the data of the selection to a global COST_METHOD

    @classmethod
    def _get_cost_method(cls, method):
        return dict(COST_METHOD)[method]

It works, I get the second entry of the tuple. But now translation isn’t working. Before I start any further, am I on the right path? Or is there another possibility to get the right translated data from the selection?

There is a translated method on selection fields which returns a new field that can be accessed from python code (error messages, reports, etc) to obtain the translated value depending on the langauge. You can find an example usage on the party module. Here is the definition and how to use it on strings.

Hope it helps!

1 Like

Thanks! I already thought it certainly should be possible to do this way easier. My working solution is now:

cost_method = fields.Selection([
        ('fixed', 'Fixed Price'),
        ('calculation', 'Based on Work Effort'),
        ('warranty', 'Warranty'),
        ('noinvoice', 'No invoicing')
    ], 'Cost Method')
cost_method_string = cost_method.translated('cost_method')

In the report I changed the value to <record.cost_method_string> and it just works!
I now can remove the extra function and move the global selection back to the field definition.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.