Reputation: 962
I´m building a dashboard where you can cross data from a sales database.
For example, sales by seller, products by client, etc. I let the user choose both option 1 and option 2.
I get the form back to the view and annotate the model with those options:
if filter_opts.is_valid():
option_1 = filter_opts.cleaned_data["option_1"]
option_2 = filter_opts.cleaned_data["option_2"]
results = ventas.values(option_2).annotate(Count(option_1, distinct=True))
The annotation works fine and if I just print the queryset in the template
{% for case in results %}
{{ case }}
{% endfor %}
I can see it like:
{'cliente': '502 EMSUR', 'prod_nombre__count': 9}
Then in the template I want to show only the values. But I can´t tell forehand which would be the value name to do something like this:
{% for case in results %}
{{ case.option_1 }}
{{ case.option_2 }}
{% endfor %}
If I iterate over the result I can see the field name:
{% for case in results %}
{% for item in case %}
{{ item }}
{% endfor %}
{% endfor %}
How can I show the values of that fields?
Thanks!
Upvotes: 1
Views: 237
Reputation: 726
Since each case
in results
is a dictionary, you can use the property .items
inside the template to iterate through its keys and values:
{% for case in results %}
{% for item, value in case.items %}
{{ item }} - {{ value }}
{% endfor %}
{% endfor %}
Upvotes: 1