You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在QWeb报表中完整打印Many2many字段技术咨询

Alright, let's tackle this problem step by step. Printing Many2many fields in QWeb reports (matching the clean, structured look of sales quotes/invoices) is totally doable—here's how to pull it off, including your specific use case with the cie10_informed model and informed_app field.

1. Core Concept: Many2many Fields Are Record Sets

First, remember that in Odoo, a Many2many field returns a record set (a collection of related records), not a single value. So you can't just drop a t-field directly on it—you need to loop through each record in the set, just like how sales quotes iterate over order lines.

2. General Approach to Replicate Sales Module's Style

Sales quotes/invoices use tables to list line items neatly. We'll reuse that pattern for your Many2many field:

  • Use a table with a header row for column labels
  • Loop through the Many2many record set with t-foreach
  • Render each related record's fields in table cells
  • Add a fallback for empty cases
3. Specific Implementation for cie10_informed's informed_report

Here's a ready-to-adapt code snippet for your QWeb template. I'll assume your informed_app related model has fields like code, name, and description—adjust these to match your actual field names:

<!-- Inside your informed_report QWeb template -->
<div class="page">
  <!-- Your existing report content goes here -->

  <!-- Section for the Many2many field: informed_app -->
  <h3 class="mt-4">Associated Applications</h3>
  <table class="table table-condensed table-striped oe_list_table">
    <thead>
      <tr class="oe_list_header">
        <th class="text-left">Code</th>
        <th class="text-left">Application Name</th>
        <th class="text-left">Details</th>
      </tr>
    </thead>
    <tbody>
      <!-- Loop through each record in the informed_app Many2many set -->
      <tr t-foreach="o.informed_app" t-as="app_record">
        <td><span t-field="app_record.code"/></td>
        <td><span t-field="app_record.name"/></td>
        <td><span t-field="app_record.description"/></td>
      </tr>
      <!-- Handle empty Many2many case -->
      <tr t-if="not o.informed_app">
        <td colspan="3" class="text-center text-muted">No associated applications found</td>
      </tr>
    </tbody>
  </table>
</div>

Breakdown of Key Parts:

  • t-foreach="o.informed_app": Iterates over every record linked via the informed_app field (where o represents the current cie10_informed record in the report)
  • t-as="app_record": Assigns a nickname to each record in the loop, so you can reference its fields easily
  • table classes: table-condensed, table-striped, and oe_list_table match the exact styling used in Odoo's sales quotes/invoices for a consistent look
  • Empty state handling: The final <tr> shows a friendly message if there are no linked records
4. Troubleshooting Common Issues

If you're still not seeing the field content, check these quick fixes:

  • Permissions: Ensure the report's security group has read access to both the cie10_informed model's informed_app field and all the fields you're trying to print from the related model
  • Field Existence: Double-check that the fields you're referencing (like app_record.code) actually exist on the model linked by informed_app
  • Report Context: Confirm your report's XML has model="cie10.informed" (adjust the XML ID to match your model's actual external ID) so o correctly points to your record
  • Test Data: Make sure the cie10_informed record you're testing has at least one linked record in informed_app—empty sets will show the fallback message

内容的提问来源于stack exchange,提问作者ayar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:32:00