Changing timezone on an existing Django project

隐身守侯 提交于 2019-12-10 13:43:44

问题


Like an idiot, I completely overlooked the timezone setting when I first built an application that collects datetime data.

It wasn't an issue then because all I was doing was "time-since" style comparisons and ordering. Now I need to do full reports that show the actual datetime and of course, they're all stored at America/Chicago (the ridiculous Django default).

So yes. I've got a medium sized database full of these dates that are incorrect. I want to change settings.TIME_ZONE to 'UTC' but that doesn't help my existing data.

What's the best (read: easiest, quickest) way to convert all that Model data en-masse?

(All the data is from within the past two months, so there's thankfully no DST to convert)

This project is currently on SQLite but I have another project on PostgreSQL with a similar problem that I might want to do the same on before DST kicks in... So ideally a DB-agnostic answer.


回答1:


I would do a mass update to the database tables by adding or subtracting hours to/from the datetime fields.

Something like this works in SQL Server, and adds 2 hours to the date:

update tblName set date_field = dateadd("hh", 2, data_field)


来源:https://stackoverflow.com/questions/689831/changing-timezone-on-an-existing-django-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!