Hello Bill, I managed to solve this in the end, the issue was that a cursor created in Django would enforce TIME_ZONE settings in the PostgreSQL session, where by default it is set to 'UTC' in my case. This means that all date manipulation was impacted, hence the unexpected results. The work around is to set the SQL session settings just before running the query with something like: cursor.execute("SET TIMEZONE to UTC")
There is a nice explanation about this here: http://blog.untrod.com/2016/08/actually-understanding-timezones-in-postgresql.html Quoting : "For instance, when Django connects to Postgres, it 'ensures timezone <https://github.com/django/django/blob/master/django/db/backends/postgresql/base.py#L195>' and slaps the application time zone onto the psycopg2 connection so you get back timestamps in the same timezone you put them in (assuming you are inserting local times). That's clever, but can lead to incredibly confusion if you are trying to compare results from the same database, but via 2 different connections, which are set to two different time zones." And a similar bug ticket : https://code.djangoproject.com/ticket/23524 Hope this will help someone else, Le mardi 11 septembre 2018 17:19:38 UTC+2, Bill-Torcaso-Oxfam a écrit : > > > I'd be interested to see a printout of 'columns' and 'cursor.description'. > > One explanation would be that your for-loop is not actually accessing the > data that you think it is, or that your query is not actually fetching the > data that you think it is. > > No criticism of your query implied - just trying to get more visibility > into the actual results. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac2e7a1c-a1e6-411c-a3ca-a1746e531e0c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.