Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created May 5, 2011 13:28
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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 %}
@gustavklopp
Copy link

Thanks! It helps me a lot!

@jordij
Copy link

jordij commented Oct 21, 2015

Thanks for sharing

@dfrojas
Copy link

dfrojas commented Feb 20, 2019

Thanks.

@Naharul98
Copy link

Thank you so much!!!!

@onorbumbum
Copy link

Thank you very much, this saved me a bunch of time.

@tochimclaren
Copy link

This is nice, you can really nest with the reqgroup template tag. One of the best default templatetags

@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