改变现有Django项目的时区 [英] Changing timezone on an existing Django project

查看:159
本文介绍了改变现有Django项目的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像一个白痴一样,当我第一次构建收集日期时间数据的应用程序时,我完全忽视了时区设置。



这不是一个问题,因为我是所有的做时间的风格比较和排序。现在我需要做完整的报告来显示实际的日期时间,当然这些都存储在美国/芝加哥(可笑的Django默认)。



所以是的。我有一个中等大小的数据库,这些日期是不正确的。我想将 settings.TIME_ZONE 更改为'UTC',但这并不能帮助我现有的数据。 >

什么是最好的(阅读:最简单,最快捷的方式)转换所有模型数据?



(全部数据来自于过去两个月,所以幸运的是没有DST转换)



这个项目目前在SQLite上,但是在PostgreSQL上有一个类似问题的另一个项目我可能想在DST踢之前做同样的事情...所以理想的是一个不可知数的答案。

解决方案

我将通过向/从datetime字段添加或减去时间来对数据库表进行批量更新。



这样的东西在SQL Server中工作,并添加了2个小时日期:

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


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.

解决方案

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)

这篇关于改变现有Django项目的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆