Reputation: 5257
I need to regroup a list, than to show the results ordered by the number of items per result's list.
something like this:
{% regroup car_models by make as car_models_by_make %}
{% for make in car_models_by_make|dictsort:"???" --> the problem %}
{{ make.grouper }} ({{ make.list|length }}) <br>
{% endfor %}
so if the data is:
[
('panda','fiat'),
('500','fiat'),
('focus','ford')
]
the result should be:
fiat 2
ford 1
I tried: dictsort:"make.list.count","make.list.length","make.list|length" .. none did the work..
Upvotes: 1
Views: 2375
Reputation: 5257
Using this template tags I've made:
@register.filter()
def groups_sort(groups):
groups.sort(key=lambda group: len(group['list']))
return groups
@register.filter()
def groups_sort_reversed(groups):
groups.sort(key=lambda group: len(group['list']), reverse=True)
return groups
It is possible to do this:
{% load custom_templatetags %}
{% regroup car_models by make as car_models_by_make %}
{% for make in car_models_by_make|groups_sort_reversed %}
{{ make.grouper }} ({{ make.list|length }}) <br>
{% endfor %}
will give you
fiat 2
ford 1
Using groups_sort will give you:
ford 1
fiat 2
this of course works on Django Models too
Enjoy
Upvotes: 1
Reputation: 30453
You should pass your data to the template as a sorted list of dicts:
>>> data = [ ('panda','fiat'),
... ('500','fiat'),
... ('focus','ford') ]
>>>
>>> from itertools import groupby
# note that we need to sort data before using groupby
>>> groups = groupby(sorted(data, key=lambda x: x[1]), key=lambda x:x[1])
# make the generators into lists
>>> group_list = [(make, list(record)) for make, record in groups]
# sort the group by the number of car records it has
>>> sorted_group_list = sorted(group_list, key=lambda x: len(x[1]), reverse=True)
# build the final dict to send to the template
>>> sorted_car_model = [{'make': make, "model": r[0]} for (make, records) in sorted_group_list for r in records]
>>> sorted_car_model
[{'make': 'fiat', 'model': 'panda'}, {'make': 'fiat', 'model': '500'}, {'make': 'ford', 'model': 'focus'}]
The goal here is to sort the list such that the car makers are ranked by the numbers of models they produce.
then, use:
{% regroup car_models by make as make_list %}
{% for item in make_list %}
{{ item.grouper }} ({{ item.list|length }}) <br>
{% endfor %}
to get:
fiat 2
ford 1
Referece: regroup
If your data looks like the one you have in your comments:
>>> car_models = [
... {'make': 'ford', 'model': 'focus'},
... {'make': 'fiat', 'model': 'panda'},
... {'make': 'ford', 'model': '500'},
... {'make': 'fiat', 'model': '500'},
... {'make': 'opel', 'model': '500'},
... {'make': 'opel', 'model': '500'},
... {'make': 'opel', 'model': '500'},
... ]
>>>
>>> groups = groupby(sorted(car_models, key=lambda x:x['make']), key=lambda x:x['make'])
>>> groups = [(make, list(x)) for make, x in groups]
>>> sorted_group_list = sorted(groups, key=lambda x:len(x[1]), reverse=True)
>>> sorted_car_model = [{'make': make, 'model': r['model']} for (make, records) in sorted_group_list for r in records]
>>> sorted_car_model
[{'make': 'opel', 'model': '500'}, {'make': 'opel', 'model': '500'}, {'make': 'opel', 'model': '500'}, {'make': 'fiat', 'model': 'panda'}, {'make': 'fiat', 'model': '500'}, {'make': 'ford', 'model': 'fo
cus'}, {'make': 'ford', 'model': '500'}]
Alternatively, you can just pass the results to the template like follows:
>>> from collections import Counter
>>> l = [r['make'] for r in car_models]
>>> c = Counter(l)
>>> result = [{k: v} for k,v in dict(c).iteritems()]
>>> result = sorted(result, key=lambda x: x.values(), reverse=True)
[{'opel': 3}, {'fiat': 2}, {'ford': 2}]
Then, pass this result to the template and simply just render the results instead:
{% for r in result %}
{% for k,v in r.items %}
{{ k }} {{ v }}
{% endfor %}j
{% endfor %}
you will then get:
opel 3
fiat 2
ford 2
Upvotes: 1
Reputation: 12911
I don't think dictsort
is fit here.
>>> l = [('panda', 'fiat'), ('500', 'fiat'), ('focus', 'ford')]
>>> values = [i[1] for i in l]
>>> from collections import Counter
>>> c = Counter(values)
>>> c.most_common()
[('fiat', 2), ('ford', 1)]
You can define your own template tags,
from django import template
from collections import Counter
register = template.Library()
@register.filter
def my_sort(l):
values = [i[1] for i in l]
c = Counter(values)
return c.most_common()
Upvotes: 2