django-cron

raw JSON →
0.6.0 verified Fri May 01 auth: no python

A Django app for running cron jobs within your Django project. Current version 0.6.0, with support for Django 4.0. Release cadence is sporadic, last release in 2023.

pip install django-cron
error 'NoneType' object has no attribute 'utcoffset'
cause This error occurs when using Django 2.0+ with django-cron < 0.5.1 due to changes in datetime handling.
fix
Upgrade to django-cron >= 0.5.1.
error django_cron.models.CronJobBase.MultipleObjectsReturned
cause Having duplicate cron jobs with the same 'code' in the database. The runcrons command expects uniqueness.
fix
Ensure each CronJob class has a unique 'code' attribute, and delete duplicate entries from the cron_jobs database table.
breaking Django 4.0 support requires django-cron >= 0.6.0. Older versions will fail on Django 4.0 due to removed utc/unaware datetime handling.
fix Upgrade to django-cron >= 0.6.0.
gotcha RUN_EVERY_MINS must be an integer. Setting it to a float like 0.5 will raise an error because the lock file mechanism expects an integer.
fix Use integer minutes, e.g., RUN_EVERY_MINS = 1 instead of 0.5.
deprecated The 'RUN_EVERY_MINS' attribute is deprecated in favor of specifying the schedule via a 'Schedule' object with 'run_every_mins' or 'run_at_times'. In future versions, using just RUN_EVERY_MINS may stop working.
fix Define a 'schedule' attribute using django_cron.Schedule instead of relying on the old RUN_EVERY_MINS class attribute.

Define a cron job class, then run 'python manage.py runcrons' to execute jobs that are due.

from django_cron import CronJobBase, Schedule

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 120  # every 2 hours
    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'my_app.my_cron_job'  # a unique code

    def do(self):
        # your logic here
        print("Cron job executed")