Can we have Django DateTimeField without timezone?

15,310

When you set USE_TZ = True(see USE_TZ for more info) in your settings, Django stores date and time information in UTC in the database otherwise it will store naive date time (date time without timezone).

The default settings.py file created by django-admin startproject includes USE_TZ = True for convenience.

So you have to set USE_TZ = False in your settings to avoid attaching timezone.

NOTE: However you cannot detach timezone only for a particular field. By following my suggestion above, you detach timezone from the entire database, so my guess is that it's better to use a CharField to store date without timezone.

You can try to override the default save handler and remove the timezone from DatetimeField before saving the item into the database, by defining a save method for your model:

def save(self, *args, **kwargs):
    self.datetime_field = self.datetime_field.replace(tzinfo=None)
    super(MyModel, self).save(*args, **kwargs)
Share:
15,310
Sagar
Author by

Sagar

Updated on June 06, 2022

Comments

  • Sagar
    Sagar almost 2 years

    I have searched for this, and could not find any notes or tutorials.

  • mehmetminanc
    mehmetminanc about 8 years
    Which is the default, I believe.
  • Sagar
    Sagar about 8 years
    @Andriy I want the date and time format this way '2016-03-09 6:51:43.195486'
  • Sagar
    Sagar about 8 years
    @Andriy I use query to insert into the table (not using django ORM) , any python functions would do for me.
  • Andriy Ivaneyko
    Andriy Ivaneyko about 8 years
    @Sagar well, try to replace tz_info to None before saving it, datetime_field = datetime_field.replace(tzinfo=None)
  • Sagar
    Sagar about 8 years
    @Andriy , This is working fine . But in database (postgresql) we get time stamp with timezone , that need to be like ' time stamp without timezone' , Anything under migrations only not from outside migrations
  • user2230901
    user2230901 over 5 years
    save should not return the object