[Django]-How to get local datetime from django model DateTimeField?

5👍

There are three possible places where you can control the timezone of datetime fields (assuming USE_TZ = True):

  • If your database doesn’t support time zones (i.e. is not PostgreSQL) you can use the database TIME_ZONE setting to set the timezone that will be used for both storage and retrieval of datetimes.

  • Your database or database adapter might have a setting that allows you to control the timezone on a per-connection basis. (That is, it doesn’t affect the storage of the datetime, it’s just a conversion of the returned data.) Django won’t change the value returned from the database adapter, so if it’s in something other than UTC Django will leave it that way.

  • You could do something within Django to automate the conversion of the returned UTC datetime to TIME_ZONE.

There are several possible ways to do the conversion within Django depending on how general you want the solution to be. For example, if you were just going to access fields on individual instances an additional model method or property would work (e.g. model.datetimeColumnTz()).

The most general way would be to make a custom model field that returned the datetime in the timezone you want. This is untested, but something like this should work:

class DateTimeFieldTz(models.DateTimeField):

    def from_db_value(self, value, expression, connection, context):
        if value is None:
            return None
        else:
            return django.utils.timezone.localtime(value)

Leave a comment