﻿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
27812	When there is a DateTimeField django does not group by	Fabio García Sánchez	nobody	"I have 2 models, both have almost the same fields. Just one of them has a DateTimeField.
The problem comes when I try to group by (not on the DateTimeField) and summarize using Sum in annotate. The model that does not have the DateTimeField works properly, the other groups by day, the Sum is per day.

{{{
qs1 = Bills.objects.values('tipo_documento').annotate(total=Sum('venta_neto'))
print(qs1 )
<QuerySet [{'tipo_documento': 'FV', 'total': Decimal('8419159.0000')}, {'tipo_documento': 'FY', 'total': Decimal('12500000.0000')}, {'tipo_documento': 'FY', 'total': Decimal('210000.0000')}...
}}}

debug_toolbar shows, effectively is grouping by ""document_date"", but I have just ""tipo_documento"" in values:
{{{
SELECT `biable_facturasbiable`.`tipo_documento`, SUM(`biable_facturasbiable`.`venta_neto`) AS `Total` FROM `biable_facturasbiable` GROUP BY `biable_facturasbiable`.`tipo_documento`, `biable_facturasbiable`.`document_date` ORDER BY `biable_facturasbiable`.`document_date` DESC LIMIT 21
}}}

this repeat the type ('tipo_documento'), each Total is per day.

On the other hand, the model without DateTimeField work properly:
{{{
qs2 = Bills2.objects.values('tipo_documento').annotate(total=Sum('venta_neto'))
print(qs2 )
<QuerySet [{'tipo_documento': 'FV', 'total': Decimal('22367372215.1000')}, {'tipo_documento': 'NV', 'total': Decimal('-38955434.0000')}, {'tipo_documento': 'NI', 'total': Decimal('0.0000')}, {'tipo_documento': 'FY', 'total': Decimal('10155529934.9700')}]>
}}}

qs2 works properly. I have tried to do the same example with another models and I have got the same issue.

debug_toolbar shows:

{{{
SELECT `biable_movimientoventabiable`.`tipo_documento`, SUM(`biable_movimientoventabiable`.`venta_neto`) AS `Total` FROM `biable_movimientoventabiable` GROUP BY `biable_movimientoventabiable`.`tipo_documento` ORDER BY NULL LIMIT 21
}}}


Thanks"	Bug	closed	Database layer (models, ORM)	1.10	Normal	invalid	queryset, group by		Unreviewed	0	0	0	0	0	0
