﻿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
25937	Failure when using expressions.DateTime on NULL values and aggregating	Thomas Recouvreux		"Running query like `qs.annotate(time=DateTime('removed', 'month', timezone.UTC())).values('time').annotate(Count('pk'))` fails with `<repr(<gill.contrib.sales.models.TransactionRowQuerySet at 0x10cc99320>) failed: ValueError: Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?>`. I noted the failure on MySQL and PostgreSQL, Django 1.8.X and 1.9.X. I did not try on other db backends or other Django versions.


To reproduce the behaviour:

1. create a model:
{{{
class Potato(models.Model):
    ..
    removed = models.DateTimeField(null=True, blank=True)
}}}

2. Insert some of them
{{{
Potato.objects.create(removed=timezone.now())
Potato.objects.create(removed=None)
..
}}}

3. Fire the request 
{{{
# Failure
Potato.objects.annotate(time=DateTime('removed', 'month', timezone.UTC())).values('time').annotate(c=Count('pk'))

# Success
Potato.objects.filter(removed__isnull=False).annotate(time=DateTime('removed', 'month', timezone.UTC())).values('time').annotate(c=Count('pk'))
}}}


Note: I printed the raw sql generated by Django and fired it directly in a db shell without any error, so the problem seems to come from the code Django uses to convert the result from the backend to python code, especially the line with NULL coming from the aggregation:

{{{
+---------------------+-----------+
| time                | pk__count |
+---------------------+-----------+
| NULL                |        17 |
| 2015-01-01 00:00:00 |         7 |
..
}}}



"	Bug	new	Database layer (models, ORM)	1.9	Normal		aggregate timezone	cherie@… Sarah Boyce	Accepted	1	0	0	1	0	0
