I am trying to filter a datetime field by month and year. For some unknown reason when entering both month and year I get back an empty set returned.
Model:
class SomePost(model.Models):
timestamp = models.DateTimeField(auto_now_add=True)
Query:
p = SomePost.objects.filter(timestamp__year=2014, timestamp__month=1)
I think it is something to do with the month but I just cannot see what i am doing wrong.
Edit
Okay so I converted the model datetimefield into a datefield and the query worked.
The resulting sql was something like this:
SELECT
`a_post`.`id`,
`a_post`.`title`,
`a_post`.`slug`,
`a_post`.`text`,
`a_post`.`timestamp`,
`a_post`.`published` FROM `a_post`
WHERE EXTRACT(MONTH FROM `a_post`.`timestamp`) = 2
ORDER BY `a_post`.`timestamp` DESC;
But when i converted it back into a datetimefield the sql was:
SELECT
`a_post`.`id`,
`a_post`.`title`,
`a_post`.`slug`,
`a_post`.`text`,
`a_post`.`timestamp`,
`a_post`.`published` FROM `a_post`
WHERE EXTRACT(MONTH FROM CONVERT_TZ(`a_post`.`timestamp`, 'UTC', UTC)) = 1
ORDER BY `a_post`.`timestamp` DESC
It appears as though the conver_tz is causing the issue. If I was to remove the convert_tz function when the query will return results.
Any ideas on whats going on here?