In Odoo QWeb reports, when working with Selection fields, the value stored in the database (e.g., family_lawsuits) is returned by default. If you want to display the human-readable label (e.g., Juicios Familiares), you need to use the dict() function to map the selection field value to its label.
Here’s how you can print the label of a selection field in a QWeb report:
1. Add a Helper Method in the Model
To make the selection field's label accessible in your QWeb report, add a helper method in your model.
Example:
class YourModel(models.Model):
_name = 'your.model'
judgements = fields.Selection(
[('family_lawsuits', 'Juicios Familiares')],
string="Judgements"
)
def get_judgement_label(self, value):
"""
Returns the human-readable label for the judgements selection field.
"""
selection_dict = dict(self.fields_get()['judgements']['selection'])
return selection_dict.get(value, '')
2. Use the Helper Method in QWeb
In your QWeb template, you can now call this helper method to fetch and display the human-readable label.
Example QWeb Template:
<t t-foreach="doc.one2many_field_ids" t-as="line">
<tr>
<td>
<!-- Call the helper method to get the label -->
<t t-esc="line.get_judgement_label(line.judgements)"/>
</td>
</tr>
</t>
3. Directly Map Selection Field in QWeb (Without a Helper)
If you don’t want to define a helper method, you can directly use the dict() function in your QWeb report. However, this approach is less reusable.
Example:
<t t-foreach="doc.one2many_field_ids" t-as="line">
<tr>
<td>
<!-- Map the selection value to its label -->
<t t-esc="dict(line.fields_get()['judgements']['selection'])[line.judgements]"/>
</td>
</tr>
</t>
Note: This direct method works, but if you have many fields to convert or if you need to reuse the logic elsewhere, the helper method is better.
4. If the Selection Field Is in a Related Model
If the judgements field is in a related model (e.g., a one2many), you must use the dict() function or helper method on the related record.
Example for Related Models:
<t t-foreach="doc.one2many_field_ids" t-as="line">
<tr>
<td>
<t t-esc="dict(line.fields_get()['judgements']['selection'])[line.judgements]"/>
</td>
</tr>
</t>
5. Final Debugging Tips
- Ensure the selection field is accessible in the record you are iterating over.
- Use t-esc to evaluate Python expressions in QWeb safely.
- Use t-debug to inspect values in the report during development.
<t t-debug="line.judgements"/>
By following this approach, you’ll be able to display the human-readable label (Juicios Familiares) of the selection field in your QWeb report. Let me know if you need further assistance!