django将旧的datetime字段转换为新的1.4 datetime,并在SQLite中感知时区 [英] django converting old datetime field to new 1.4 datetime with time zone aware in SQLite

查看:321
本文介绍了django将旧的datetime字段转换为新的1.4 datetime,并在SQLite中感知时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django 1.3项目,我正在添加将按一定时间运行的文本通知。因此,我将需要该应用程序是时区知道。看起来像django 1.4处理这个很好。问题是我使用SQLite数据库,它不存储时区信息,所以我必须转换。我是一个django新手。没有关于如何将SQLlite中的datetime字段转换为时区感知字段的建议。



顺便说一句,当我升级到django 1.4并激活后启动manage.py shell时区支持,我收到以下消息:

  RuntimeWarning:SQLite收到一个天真的datetime(2012-04-05 15:18: 49.939725),而
时区支持处于活动状态。 RuntimeWarning)

谢谢!

解决方案

您不会将SQlite中的datetime字段转换为时区感知字段。在SQlite中,datetime仍然是天真的,但转换为UTC。而在应用程序级别可以使用时区感知的datetimes。



例如,当您从SQlite查询Model实例时,具有datetime字段,Django:从$ s $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ you you you you you you you you you you you you you you you you you you you you you you you you you you you you you访问model_instance.datetime_field,它已经是时区知道并已本地化到UTC

  • 然后在模板中,例如,您可以将其本地化到当前时区

  • 编辑:更正了我最初错误的答案,了解您在模型实例上的内容。



    如果当前的SQlite数据库中的数据时间不是UTC,那么您需要进行一次迁移,将它们全部转换为UTC。 (和UTC,我只是意味着天真的UTC值。由于SQlite和MySQL - 没有datetime字段存储时区信息)



    我建议你阅读时区相关文档非常仔细和彻底: https://docs.djangoproject.com/en/ 1.4 / topics / i18n / timezones /



    很容易出错。



    是的,你会得到 RunTimeWarning 的每个实例,你的代码给天真的数据时间Django保存。您将不得不逐渐修复这些问题,从而使其具有时区感知能力。


    I have a django 1.3 project and I'm in the process of adding text notifications that will run on a certain schedule. As such, I'm going to need the app to be timezone aware. It looks like django 1.4 handles this pretty well. THe problem is that I am using SQLite for db, and it doesn't store the timezone info, so I have to convert. I'm a django newbie. ANy suggestions on how to convert datetime fields in SQLlite to timezone-aware fields.

    By the way, when I launch the manage.py shell after upgrading to django 1.4 and activating timezone support, I get the following message:

    RuntimeWarning: SQLite received a naive datetime (2012-04-05 15:18:49.939725) while 
    time zone support is active. RuntimeWarning)
    

    Thanks!

    解决方案

    You don't convert datetime fields in SQlite to timezone-aware fields. Within SQlite the datetime is still going to be naive but converted to UTC. And the timezone aware datetimes ara available at the application level.

    For example, when you query for a Model instance from SQlite, that has a datetime field, Django:

    1. Gets the naive datetime from SQLite, sets it to aware datetime with UTC timezone
    2. So then when you access the model_instance.datetime_field, it's already timezone aware and localized to UTC
    3. Then in templates for example, you can localize it to current timezone

    EDIT: Corrected my initially wrong answer, about what you get on the model instance.

    If currently the datetimes in your SQlite db are not UTC, you'll need to do a one-time migration to convert them all to UTC. (and by UTC I just mean naive UTC value. As SQlite - and also MySQL - do not have datetime fields that store timezone info)

    I suggest you read the timezone related documentation very carefully and thoroughly: https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/

    It's very easy to get this wrong.

    Yes you'll get RunTimeWarning for every instance where your code "gives" naive datetimes to Django for saving. You'll have to fix those gradually, make them timezone-aware.

    这篇关于django将旧的datetime字段转换为新的1.4 datetime,并在SQLite中感知时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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