Skip to content

Instantly share code, notes, and snippets.

@zstumgoren
Created October 14, 2011 20:14
Show Gist options
  • Save zstumgoren/1288192 to your computer and use it in GitHub Desktop.
Save zstumgoren/1288192 to your computer and use it in GitHub Desktop.
base_query = Casualty.safe_objects.all()
def build_chart(base_query, field_name, order_by):
'''
This is a function to make building our charts a little DRYer. Beats the pants off of lambdas.
'''
return base_query.values(field_name).annotate(Count(field_name)).order_by(order_by)
extra_context_dict = {
'theater_chart':build_chart(base_query, 'theater__name', '-theater__name__count'),
'branch_chart':build_chart(base_query, 'military_service_branch__name', '-military_service_branch__name__count'),
'year_chart':build_chart(base_query, 'death_date_year', '-death_date_year'),
'sex_chart':build_chart(base_query, 'sex', '-sex__count'),
'age_chart':build_chart(base_query, 'age_range', '-age_range__count'),
'cause_chart':build_chart(base_query, 'cause_of_death__name', '-cause_of_death__name__count'),
'total_count':base_query.count(),
'now':datetime.datetime.now(),
'timeline':base_query.values('death_date_year').annotate(Count('death_date_year')).order_by('-death_date_year'),
}
# Lots 'o inheritance below...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment