[Django]-Django DateTime field to generate timestamp fields withOUT timezone

4👍

The data types for postgres in Django map to the string literal timestamp with time zone, and there doesn’t seem to be an option to alter that type.

As far as I’m aware, you have three options:

  1. Take advantage of Django’s hook for executing raw sql after the create table statement. To do this create a directory called sql in your app and a file in that directory called mymodel.postgres_psycopg2.sql. Place your alter table statements in there.

  2. Write a custom field to define the modifiers for the timestamp field as you see fit. See “writing custom model fields“.

  3. Leave the django field as is, and perform the timezone conversion in your application so that you are absolutely certain you are passing the correct time.

My personal preference for data integrity is #3. Just as with character encodings, where you always want to be sure you know the charset and are handling conversions properly, I feel much more comfortable with dates/times whose attributes (including TZ) are as precisely defined as they can be. You may be certain today that all your input is coming to your app already in UTC, but that may change, particular if the timestamps are supplied by end users. For what it’s worth, I’d go the extra mile, convert the timestamp in the app to UTC, and store it in the DB with UTC as the time zone.

Leave a comment