mfogel / django-timezone-field

A Django app providing DB, form, and REST framework fields for zoneinfo and pytz timezone objects.
BSD 2-Clause "Simplified" License
397 stars 96 forks source link

System timezones or installed tzdata may not cover pytz.common_timezones #85

Open paulocoutinhox opened 2 years ago

paulocoutinhox commented 2 years ago

Hi,

Im getting this error:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/paulo/Developer/workspaces/python/pyaa/manage.py", line 22, in <module>
    main()
  File "/Users/paulo/Developer/workspaces/python/pyaa/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/paulo/Developer/workspaces/python/pyaa/customer/models.py", line 12, in <module>
    from timezone_field import TimeZoneField
  File "/usr/local/lib/python3.9/site-packages/timezone_field/__init__.py", line 1, in <module>
    from timezone_field.fields import TimeZoneField
  File "/usr/local/lib/python3.9/site-packages/timezone_field/fields.py", line 11, in <module>
    class TimeZoneField(models.Field):
  File "/usr/local/lib/python3.9/site-packages/timezone_field/fields.py", line 41, in TimeZoneField
    default_zoneinfo_tzs = [ZoneInfo(tz) for tz in pytz.common_timezones]
  File "/usr/local/lib/python3.9/site-packages/timezone_field/fields.py", line 41, in <listcomp>
    default_zoneinfo_tzs = [ZoneInfo(tz) for tz in pytz.common_timezones]
  File "/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/zoneinfo/_common.py", line 24, in load_tzdata
    raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key Pacific/Kanton'

Thanks.

paulocoutinhox commented 2 years ago

I need install package tzdata to make it work.

It is really necessary?

mfogel commented 2 years ago

Hi @paulocoutinhox, what version of django are you running and on what platform?

It looks like django 4.X on windows should be installing tzdata by default. So I'm guessing you're on django 3.X on windows? Is that correct? Thanks.

paulocoutinhox commented 2 years ago

Im using latest django 4 on macos.

mfogel commented 2 years ago

Ok thanks

It looks like the timezone which is causing the problem is Pacific/Kanton was added to the IANA database in september 2021. So it makes sense that the system default list of timezones doesn't include it yet.

I'm hesitant to add a direct dependency on tzdata to this package because both python stdlib (via zoneinfo) and django choose not to do so. Let me mull it over. Perhaps we should push for django to remove the sys_platform == 'win32' part of this line.

garw commented 2 years ago

I just encountered the same issue with python 3.9 on debian bullseyse with django 3.x. I now manually fixed pytz==2021.1 for my setup to get around this.