Sorry this is basic, but I’m new to Python and Django.
I have a list of users who have performed an action that is being saved in a separate table. I want to show a list of these users, sorted by how many times they’ve taken the other action.
I understand how to get the counts:
users = User.objects.all()
for user in users:
print user.action_set.count()
And I understand how to show the counts in my template:
{% for user in users %}
{{ user.action_set.count }}
{% endfor %}
But I’m not sure how to have it sort the users by the count of the action set.
Annotate / Aggregation
http://docs.djangoproject.com/en/dev/topics/db/aggregation/
For future reference, if you couldn’t sort by the DB, you can python sort any list…
Very useful.