def group_by_period(queryset, column, period, **annotate):
 d = OrderedDict()
 for line in queryset.extra({'period_group': connection.ops.date_trunc_sql(period, column)}).values('period_group').annotate(**annotate).order_by('period_group').values(*(['period_group'] + list(annotate.keys()))):
  d[parse_date(line.pop('period_group'))] = line
 return d

def group_by_period ( queryset , column , period , ** annotate ) : d = OrderedDict ( ) for line in queryset . extra ( { 'period_group' : connection . ops . date_trunc_sql ( period , column ) } ) . values ( 'period_group' ) . annotate ( ** annotate ) . order_by ( 'period_group' ) . values ( * ( [ 'period_group' ] + list ( annotate . keys ( ) ) ) ) : d [ parse_date ( line . pop ( 'period_group' ) ) ] = line return d
group and annotate given queryset by a given date period .