Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Django 5.0.1) AttributeError at /admin/django_celery_beat/crontabschedule/add/ #711

Closed
AlexEnrique opened this issue Jan 9, 2024 · 3 comments · Fixed by #712
Closed

Comments

@AlexEnrique
Copy link

AlexEnrique commented Jan 9, 2024

Summary:

There are two places where AttributeError is raised:

  • Running celery beat with celery -A project beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler

  • Clicking in "+ Add" button for Crontabs in the Django admin raised AttributeError (traceback at the end).

Versions used

  • Python 3.12.1
  • celery==5.3.6
  • Django==5.0.1
  • django-celery-beat==2.1.0
  • django-timezone-field==4.2.3
  • Running on Docker, with image = python:3.12.1-alpine3.19

Exact steps to reproduce the issue:

  1. Install the above dependencies
  2. Run the migrate command
  3. Go to the django Admin > Periodic Tasks > Crontabs
  4. Click in the "+Add" or "+ADD CRONTAB" button

Detailed information

#699
https://docs.djangoproject.com/en/dev/releases/5.0/
https://docs.djangoproject.com/en/dev/releases/5.0.1/

Tracebacks

Clicking in the +add button

[2024-01-09 09:35:30] [   ERROR] - Internal Server Error: /admin/django_celery_beat/crontabschedule/add/ (log.py:241)
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 479, in __call__
    ret: _R = await loop.run_in_executor(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 538, in thread_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 714, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/views/decorators/cache.py", line 80, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/sites.py", line 240, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 1941, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 188, in _view_wrapper
    result = _process_exception(request, e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/decorators.py", line 186, in _view_wrapper
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 1802, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/admin/options.py", line 1869, in _changeform_view
    form = ModelForm(initial=initial)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/forms/models.py", line 362, in __init__
    self.instance = opts.model()
                    ^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 535, in __init__
    val = field.get_default()
          ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/timezone_field/fields.py", line 126, in get_default
    value = super().get_default()
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 1027, in get_default
    return self._get_default()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/models.py", line 60, in crontab_schedule_celery_timezone
    choice[0].zone for choice in timezone_field.TimeZoneField.CHOICES
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'TimeZoneField' has no attribute 'CHOICES'

On startup

celery beat v5.3.6 (emerald-rush) is starting.
__    -    ... __   -        _
LocalTime -> 2024-01-09 10:13:41
Configuration ->
    . broker -> redis://redis:6379/1
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> django_celery_beat.schedulers.DatabaseScheduler

    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 seconds (5s)
[2024-01-09 10:13:41,352: INFO/MainProcess] beat: Starting...
[2024-01-09 10:13:41,408: CRITICAL/MainProcess] beat raised exception <class 'AttributeError'>: AttributeError("'zoneinfo.ZoneInfo' object has no attribute 'localize'")
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/celery/apps/beat.py", line 113, in start_scheduler
    service.start()
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 634, in start
    humanize_seconds(self.scheduler.max_interval))
                     ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/kombu/utils/objects.py", line 40, in __get__
    return super().__get__(instance, owner)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 677, in scheduler
    return self.get_scheduler()
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 668, in get_scheduler
    return symbol_by_name(self.scheduler_cls, aliases=aliases)(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 231, in __init__
    Scheduler.__init__(self, *args, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 264, in __init__
    self.setup_schedule()
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 239, in setup_schedule
    self.install_default_entries(self.schedule)
                                 ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 361, in schedule
    self._schedule = self.all_as_schedule()
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 247, in all_as_schedule
    s[model.name] = self.Entry(model, app=self.app)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 93, in __init__
    model.last_run_at = self._default_now()
                        ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 143, in _default_now
    now = now.tzinfo.localize(now.replace(tzinfo=None))
          ^^^^^^^^^^^^^^^^^^^
AttributeError: 'zoneinfo.ZoneInfo' object has no attribute 'localize'

Also: 'django_celery_beat' have changes that are not yet reflected in a migration.

Running makemigrations created /usr/local/lib/python3.12/site-packages/django_celery_beat/migrations/0015_alter_clockedschedule_id_alter_crontabschedule_id_and_more.py

But the error still persists.

@Nigel2392
Copy link

Nigel2392 commented Jan 9, 2024

More discussed here: #698

Swapping to older versions of libraries can not only cause security issues - dependency issues, as you are noticing is also a problem. I'm pretty sure ZoneInfo.localize is deprecated in pytz.

@AlexEnrique
Copy link
Author

I totally agree. I am just pointing out that the solution to use the lib with Django 5.0 given in #699 is not working.

I would like to contribute more to support the new Django version, but I don't know from where to start (never contributed directly to open source software).

This may be the source of the first issue: Support for pytz timezones was removed in Django 5.0. The second was probably fixed between ver. 2.1.0 and 2.5.0.

@cclauss
Copy link
Contributor

cclauss commented Jan 9, 2024

Installing on Dj5.0.x is DOWNGRADING your django-celery-beat as discussed at #698 (comment)

This means that a previously fixed #378 is no longer present.

A new release to PyPI would fix the issue.

@AlexEnrique Please close this issue as a duplicate of #698 or change the title to:
Installing django-celery-beat on Django v5.0 DOWNGRADES to v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants