跳至内容
菜单
此问题已终结
3 回复
4483 查看
judgements = fields.Selection(
[
('family_lawsuits', 'Juicios Familiares')])

I have this selector that is stored inside a one2many, when I command to call the field in the qweb and print it, I want it to print the option that says "Family Lawsuits", but it prints the first option that is "family_lawsuits", how can I do to print the second option?
I already tried putting a .name at the end of the field, but it gives me the following error: AttributeError: 'str' object has no attribute 'name'


形象
丢弃
最佳答案

Hi,

Try,

<t t-esc="dict(object.fields_get(allfields=['your_selection_field'])['your_selection_field']['selection'])[object.your_selection_field]"/>


形象
丢弃
最佳答案

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!

形象
丢弃
最佳答案

You can user the following: 

map_report_type = dict(self._fields["report_type"].selection)


形象
丢弃
相关帖文 回复 查看 活动
2
6月 23
4324
0
3月 22
2356
0
8月 21
4984
0
4月 18
8462
1
12月 17
7907