如何在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.
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.
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
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 theinformed_appfield (whereorepresents the currentcie10_informedrecord in the report)t-as="app_record": Assigns a nickname to each record in the loop, so you can reference its fields easilytableclasses:table-condensed,table-striped, andoe_list_tablematch 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
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_informedmodel'sinformed_appfield 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 byinformed_app - Report Context: Confirm your report's XML has
model="cie10.informed"(adjust the XML ID to match your model's actual external ID) soocorrectly points to your record - Test Data: Make sure the
cie10_informedrecord you're testing has at least one linked record ininformed_app—empty sets will show the fallback message
内容的提问来源于stack exchange,提问作者ayar

