Created
May 5, 2011 13:28
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 %} |
Thanks.
Thank you so much!!!!
Thank you very much, this saved me a bunch of time.
This is nice, you can really nest with the reqgroup template tag. One of the best default templatetags
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
Thanks for sharing