跳转到主要内容

Django模型的时区工具

项目描述

Latest Version Test Status Coverage Status Code Health Supported Python versions License Development Status

django-timezone-utils 为Django添加了自动时区转换和支持工具。

欢迎并感谢建议、建设性批评和反馈。

文档

django-timezone-utils的文档可在Read the Docs找到。

灵感

在多次场合,我需要将时区信息存储到一个模型中,然后以该时区为基础创建另一个模型的datetime。如果你曾经处理过这种情况,你会知道这有多么复杂。

我创建了这些字段,以便于根据另一个字段或模型的时间区域选择来操作时间。现在,我们可以在模型字段中内置验证,而不是必须记住使用 Model.clean

快速示例

from datetime import datetime
from timezone_utils.fields import LinkedTZDateTimeField, TimeZoneField
from timezone_utils.choices import PRETTY_ALL_TIMEZONES_CHOICES

class Location(models.Model):
    # ...
    timezone = TimeZoneField(choices=PRETTY_ALL_TIMEZONES_CHOICES)


def get_location_timezone(obj):
    """Returns the Location.timezone field from above"""

    return obj.location.timezone


class LocationReport(models.Model):
    # ...
    location = models.ForeignKey('app_label.Location', related_name='reports')

    # Populates from the Location.timezone
    timestamp = LinkedTZDateTimeField(populate_from=get_location_timezone)


class LocationPeriod(models.Model):
    # ...
    location = models.ForeignKey('app_label.Location', related_name='periods')

    # Sets the time to 12:00am in the location.timezone
    start = LinkedTZDateTimeField(
        populate_from=get_location_timezone,
        time_override=datetime.min.time()
    )

    # Sets the time to 11:59:59.99999pm in the location.timezone
    end = LinkedTZDateTimeField(
        populate_from=get_location_timezone,
        time_override=datetime.max.time()
    )

贡献者

变更日志

  • 0.15.0 添加对 Django 4.0 的支持。放弃对 Django 1.11 的支持。

  • 0.14.0 添加对 Django 2.2、3.0 的支持。放弃对 Django 2.0、2.1 的支持。

  • 0.13 修复了在 PyPi 上 Python 3 的错误。

  • 0.12 添加对 Django 2.1 的支持。支持 Python 3.7。放弃对 Django 1.8 的支持。

  • 0.11 删除了对 django.db.models.fields.subclassing.SubfieldBase 的引用,这意味着现在只支持 Django 1.8+。删除了对 Python 版本 < 2.6 的支持。Django 1.6 系列是最后一个支持 Python 2.6 的系列。为 Django 1.10 添加了测试支持。开发状态从 Beta 更改为 Production/Stable。

  • 0.10 添加了对 Python 3.5 和 Django 1.9 的测试支持。

  • 0.9 修复了 time_override 导致由于未首先转换为正确的时间区域而出现无效日期的 bug。重构了转换代码。为 Django 1.8 添加了测试支持。从设置要求中删除了 Django - 支持的 Django 版本的责任在于开发者。

  • 0.8 修复了 time_override 导致由于未首先转换为正确的时间区域而出现无效日期的 bug。在文档中添加了选择 GROUPED_ALL_TIMEZONES_CHOICESGROUPED_COMMON_TIMEZONES_CHOICES

  • 0.7 修复了 datetime.max.time() 导致不正确的日期/时间的 bug。将测试更改为通过字符串比较时间 override 模型,以防止未来的回归。添加了选择 GROUPED_ALL_TIMEZONES_CHOICESGROUPED_COMMON_TIMEZONES_CHOICES

  • 0.6 添加了 RTD 文档。LinkedTZDateTimeField 现在在覆盖的时间区域和时间内返回 datetime 对象。

  • 0.5 Bug 修复:time override 在 datetime.min.time() 上失败,未能正确设置时间

  • 0.4 删除了对 Python 2.5 的支持

  • 0.3 代码清理。

  • 0.2 根据测试进行了多个错误修复。

  • 0.1 首次发布。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

django-timezone-utils-0.15.0.tar.gz (8.7 kB 查看哈希)

上传

构建分布

django_timezone_utils-0.15.0-py2.py3-none-any.whl (9.8 kB 查看哈希)

上传 Python 2 Python 3

由以下支持