Django 时区字段不接受所有 pytz 时区

Django timezone fields don't accept all pytz timezones

Django timezone fields are built upon pytz timezones,但后者的一些时区对字段无效。使用

这样的模型
from django.db import models
from timezone_field import TimeZoneField

class TestModel(models.Model):
    timezone_field = TimeZoneField(default='UTC')

def save(self, *args, **kwargs):
    self.clean()
    self.full_clean()
    super().save(*args, **kwargs)

如果我运行(在shell)

import pytz
from models import TestModel

model = TestModel.objects.get(id=1)

for zone in pytz.all_timezones:
    model.timezone = zone
    model.save()

我明白了

django.core.exceptions.ValidationError: {'timezone': ["Value <DstTzInfo 'Africa/Asmera' LMT+2:27:00 STD> is not a valid choice."]}

所以它首先在 'Africa/Asmera' 上失败(它在其他地方也失败,例如 'GMT+0')。

知道如何解决这种不一致吗?目前,用户可以 select 前端的时区会给出后端错误(前端是在 React 中构建的,并从 moment timezone 获取时区)

正如 Willem 在 Django Timezone Fields 的 V1.2 中提到的,他们将字段的默认选择从 all_timezones 更改为 common_timezones。您可以通过在字段定义中显式设置 choices 属性来恢复此更改。

https://github.com/mfogel/django-timezone-field

1.2 (2015-02-05) For form field, changed default list of accepted timezones from pytz.all_timezones to pytz.common_timezones, to match DB field behavior. 1.1 (2014-10-05) Django 1.7 compatibility Added support for formating choices kwarg as [[, ], ...], in addition to previous format of [[, ], ...]. Changed default list of accepted timezones from pytz.all_timezones to pytz.common_timezones. If you have timezones in your DB that are in pytz.all_timezones but not in pytz.common_timezones, this is a backward-incompatible change. Old behavior can be restored by specifying choices=[(tz, tz) for tz in pytz.all_timezones] in your model definition.

class TestModel(models.Model):
    timezone_field = TimeZoneField(default='UTC', choices=[(tz, tz) for tz in pytz.all_timezones])

关于这是向后不兼容的更改有免责声明;所以我敦促你进一步研究