0

I moved from not using timezone to using timezone but I have a couple of questions about the handling of it. First of all my settings look like this:

TIME_ZONE = "Europe/Amsterdam"
USE_TZ = True

I now get the correct timezone back from the database but not in the right timezone. I suspect that when I have my TIME_ZONE on Europe/Amsterdam that I get that timezone back. But instead I get:

2020-09-27 23:00:07+00:00

But what I want to receive is:

2020-09-27 22:00:00+02:00

I can do this by using

timezone.localtime(date_object)

But is there a way to get the timezone back in the right format straight from the orm?

I am using Postgres 11 and Django 3.0.1

  • What is the type for the field you are storing this in? How is this field specified in the model? Where are you getting the above value, raw query, view, etc? – Adrian Klaver Sep 29 '20 at 14:12
  • Basically, no, unless you want to create your own model field. Django converts times on input and output (forms and templates), but doesn't do any conversions on the datetime code objects returned by the database adapter. That's because conversions can lose information (e.g. ambiguous times in DST). – Kevin Christopher Henry Sep 29 '20 at 14:51

1 Answers1

1

But is there a way to get the timezone back in the right format straight from the orm?

No, Django always gives the datetime objects in UTC; see this answer. Django refers to the TIME_ZONE when rendering templates or parsing forms, but before that, everything's in UTC unless explicitly transformed otherwise.

DJ Ramones
  • 823
  • 1
  • 5
  • 11