Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created May 5, 2011 13:28
Show Gist options
  • Save ChrisLTD/957014 to your computer and use it in GitHub Desktop.
Save ChrisLTD/957014 to your computer and use it in GitHub Desktop.
Django Nested Regroup Example (Group by category foreign key, then month of start date)
#views.py
def events_index(request, year):
selected_year = Year.objects.get(title=year)
events_list = Event.objects.filter(year = selected_year.id).order_by('category','start_date')
return render_to_response('events_list.html', {"events_list": events_list})
#events_list.html
{% regroup events_list by category.title as events_list_by_category %}
{% for category in events_list_by_category %}
<hr>
<h2>{{ category.grouper }}</h2>
{% regroup category.list by start_date.month as events_list_by_month %}
{% for month in events_list_by_month %}
<h3>{{ month.grouper }}</h3>
{% for event in month.list %}
<h4>{{ event.title }}</h4>
<p>{{ event.start_date }}</p>
<p>{{ event.category.title }}</p>
{% endfor %}
{% endfor %}
{% endfor %}
@ladypython247
Copy link

This does not work well with postgresql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment