3
votes

Edit: So I dropped this and then waited for a few days it started working! Some how the upgrade to 1.6 took a while to 'propagate'! shrugs. Thanks to all who chimed in!

The queryset filter month does not seem to be working correctly. I have a bunch of objects in database with model called Note with field pub_date storing a datetime object. I want to retrieve Note objects by month. So here is a test I did:

>>> from blogengine.models import Note
>>> n = Note.objects.all()[0]
>>> n.pub_date
datetime.datetime(2014, 3, 8, 21, 15, 14, tzinfo=<UTC>)

>>> Note.objects.filter(pub_date__year = 2014)
[<Note: Note object>, <Note: Note object>]

>>> Note.objects.filter(pub_date__month =3)
[]

As you can see the year look up works correctly, giving me the two objects with year=2014, but the month lookup returns nothing even though there is an object with that month as can be seen from the first example object n. This also happens for all other datetime lookups like day or minute.

Python = 2.7.5 Django 1.6.2

2
"When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database" did you check your configuration? - fixmycode
@ONi Yes, all my datetimes in database are UTC. But I don't understand why that will affect month and not year? - Kartik Prabhu
It might be that some data is getting lost in translation. Try printing the queryset's query attribute: Note.objects.filter(pub_date__month =3).query - fixmycode
so that gives me: <django.db.models.sql.query.Query object at 0x3c38110> But what does that mean? - Kartik Prabhu
I should've said it clearer: print Note.objects.filter(pub_date__month =3).query - fixmycode

2 Answers

1
votes

I had a similar problem which turned out to be mysql not loading tz info. See here: https://stackoverflow.com/a/14454465/8092

0
votes

So I dropped this and then waited for a few days it started working! Some how the upgrade to 1.6 took a while to 'propagate'! shrugs.

Thanks to all who chimed in!