﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26649	Group by date_trunc (day, month, year) without extra	Till Backhaus	nobody	"This seems like a regression resulting from the deprecation of .extra.
Grouping things by different time ranges is relevant to many django users. It seems this cannot be done without the use of extra right now. 

The closest I got was 

{{{
Sales.objects.datetimes('timestamp', 'month').annotate(c=Count('id')).values('c')
(0.001) SELECT DISTINCT 
    COUNT(""group_sales_sales"".""id"") AS ""c"", 
    DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC') 
FROM ""group_sales_sales"" 
WHERE ""group_sales_sales"".""timestamp"" IS NOT NULL 
GROUP BY DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC') 
ORDER BY DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC') ASC; 
args=('UTC', 'UTC', 'UTC')
<QuerySet [{'c': 1}, {'c': 3}]>
}}}

Please note that the sql looks good. It's just that I cannot get the month from the annotation without django falling back to the wrong query:
{{{
Sales.objects.datetimes('timestamp', 'month').annotate(c=Count('id')).values('timestamp','c')
(0.001) SELECT DISTINCT 
    ""group_sales_sales"".""timestamp"", 
    COUNT(""group_sales_sales"".""id"") AS ""c"", 
    DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC') 
FROM ""group_sales_sales"" 
WHERE ""group_sales_sales"".""timestamp"" IS NOT NULL 
GROUP BY DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC'), 
""group_sales_sales"".""timestamp"" ORDER BY DATE_TRUNC('month', ""group_sales_sales"".""timestamp"" AT TIME ZONE 'UTC') ASC LIMIT 21; args=('UTC', 'UTC', 'UTC')

Out[10]: <QuerySet [{'timestamp': datetime.datetime(2016, 4, 1, 17, 19, 44, tzinfo=<UTC>), 'c': 1}, {'timestamp': datetime.datetime(2016, 5, 22, 17, 19, 39, tzinfo=<UTC>), 'c': 1}, {'timestamp': datetime.datetime(2016, 5, 22, 17, 19, 23, tzinfo=<UTC>), 'c': 1}, {'timestamp': datetime.datetime(2016, 5, 22, 17, 19, 33, tzinfo=<UTC>), 'c': 1}]>
}}}


{{{ 
#model:
class Sales(models.Model):
    something = models.CharField(max_length=32)
    timestamp = models.DateTimeField()
}}}"	Uncategorized	closed	Database layer (models, ORM)	1.10	Normal	fixed	Queryset.extra	josh.smeaton@…	Unreviewed	0	0	0	0	0	0
