简体   繁体   中英

django report from model data - print multiple tables

I'm trying to print out a report in table format; but need to break the table and start a new one when a specific column changes.

Example Data (in a Model)

item1 value group1
item2 value group1
item3 value group2
item4 value group3

output:

For Group1
- Item1 value
- Item2 value
For Group2
- Item3 value
For Group3
- Item4 value

I get this data from a mysql database... I'm trying output this data in a Template and do all the formatting with template using tags... i tried ifchanged, ifequal (first getting a list of unique groups)... but neither work well (or at all)...

what would be the most efficient way to do this (as there could be several hundred records over numerous/countless groups - so I figured it isn't good to make numerous calls to the database and build an array of Models that contain an array of each group results

Thoughts?

You can use a loop in a template:

{% for item in group1 %}
    {{ item.value }}
{% endfor %}

{% for item in group2 %}
    {{ item.value }}
{% endfor %}

{% for item in group3 %}
    {{ item.value }}
{% endfor %}

If you could post the code from your view that would help me answer the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM