Skip to content

Abstract Django DB Models #911

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ coverage.xml
.python-version
venv
.env
.vscode/
48 changes: 48 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,54 @@ manually:
>>> from django_celery_beat.models import PeriodicTasks
>>> PeriodicTasks.update_changed()

Custom Models
=============

It's possible to use your own models instead of the default ones provided by ``django_celery_beat``, to do that just define your models inheriting from the right one from ``django_celery_beat.models.abstract``:

.. code-block:: Python

# custom_app.models.py
from django_celery_beat.models.abstract import (
AbstractClockedSchedule,
AbstractCrontabSchedule,
AbstractIntervalSchedule,
AbstractPeriodicTask,
AbstractPeriodicTasks,
AbstractSolarSchedule,
)

class CustomPeriodicTask(AbstractPeriodicTask):
...

class CustomPeriodicTasks(AbstractPeriodicTasks):
...

class CustomCrontabSchedule(AbstractCrontabSchedule):
...

class CustomIntervalSchedule(AbstractIntervalSchedule):
...

class CustomSolarSchedule(AbstractSolarSchedule):
...

class CustomClockedSchedule(AbstractClockedSchedule):
...

To let ``django_celery_beat.scheduler`` make use of your own modules, you must provide the ``app_name.model_name`` of your own custom models as values to the next constants in your settings:

.. code-block:: Python

# settings.py
# CELERY_BEAT_(?:PERIODICTASKS?|(?:CRONTAB|INTERVAL|SOLAR|CLOCKED)SCHEDULE)_MODEL = "app_label.model_name"
CELERY_BEAT_PERIODICTASK_MODEL = "custom_app.CustomPeriodicTask"
CELERY_BEAT_PERIODICTASKS_MODEL = "custom_app.CustomPeriodicTasks"
CELERY_BEAT_CRONTABSCHEDULE_MODEL = "custom_app.CustomCrontabSchedule"
CELERY_BEAT_INTERVALSCHEDULE_MODEL = "custom_app.CustomIntervalSchedule"
CELERY_BEAT_SOLARSCHEDULE_MODEL = "custom_app.CustomSolarSchedule"
CELERY_BEAT_CLOCKEDSCHEDULE_MODEL = "custom_app.CustomClockedSchedule"

Example creating interval-based periodic task
---------------------------------------------

Expand Down
135 changes: 135 additions & 0 deletions django_celery_beat/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


def crontabschedule_model():
"""Return the CrontabSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_CRONTABSCHEDULE_MODEL"):
from .models.generic import CrontabSchedule

return CrontabSchedule

try:
return apps.get_model(settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL}' that has not "
"been installed"
)


def intervalschedule_model():
"""Return the IntervalSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_INTERVALSCHEDULE_MODEL"):
from .models.generic import IntervalSchedule

return IntervalSchedule

try:
return apps.get_model(settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL}' that has not "
"been installed"
)


Comment on lines +5 to +49
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The model‐fetching functions in this file have nearly identical logic. Consider extracting the common pattern into a single helper that takes the setting name and default import path to reduce duplication.

Suggested change
def crontabschedule_model():
"""Return the CrontabSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_CRONTABSCHEDULE_MODEL"):
from .models.generic import CrontabSchedule
return CrontabSchedule
try:
return apps.get_model(settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL}' that has not "
"been installed"
)
def intervalschedule_model():
"""Return the IntervalSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_INTERVALSCHEDULE_MODEL"):
from .models.generic import IntervalSchedule
return IntervalSchedule
try:
return apps.get_model(settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL}' that has not "
"been installed"
)
def get_model_or_default(setting_name, default_model_path):
"""Fetch a model based on a setting name or return the default model."""
if not hasattr(settings, setting_name):
module_path, model_name = default_model_path.rsplit(".", 1)
module = __import__(module_path, fromlist=[model_name])
return getattr(module, model_name)
try:
return apps.get_model(getattr(settings, setting_name))
except ValueError:
raise ImproperlyConfigured(
f"{setting_name} must be of the form 'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
f"{setting_name} refers to a model "
f"'{getattr(settings, setting_name)}' that has not been installed"
)
def crontabschedule_model():
"""Return the CrontabSchedule model that is active in this project."""
return get_model_or_default(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL",
"django_celery_beat.models.generic.CrontabSchedule"
)
def intervalschedule_model():
"""Return the IntervalSchedule model that is active in this project."""
return get_model_or_default(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL",
"django_celery_beat.models.generic.IntervalSchedule"
)

Copilot uses AI. Check for mistakes.

def periodictask_model():
"""Return the PeriodicTask model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_PERIODICTASK_MODEL"):
from .models.generic import PeriodicTask

return PeriodicTask

try:
return apps.get_model(settings.CELERY_BEAT_PERIODICTASK_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASK_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASK_MODEL refers to model "
f"'{settings.CELERY_BEAT_PERIODICTASK_MODEL}' that has not been "
"installed"
)


def periodictasks_model():
"""Return the PeriodicTasks model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_PERIODICTASKS_MODEL"):
from .models.generic import PeriodicTasks

return PeriodicTasks

try:
return apps.get_model(settings.CELERY_BEAT_PERIODICTASKS_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASKS_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASKS_MODEL refers to model "
f"'{settings.CELERY_BEAT_PERIODICTASKS_MODEL}' that has not been "
"installed"
)


def solarschedule_model():
"""Return the SolarSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_SOLARSCHEDULE_MODEL"):
from .models.generic import SolarSchedule

return SolarSchedule

try:
return apps.get_model(settings.CELERY_BEAT_SOLARSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_SOLARSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_SOLARSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_SOLARSCHEDULE_MODEL}' that has not been "
"installed"
)


def clockedschedule_model():
"""Return the ClockedSchedule model that is active in this project."""
if not hasattr(settings, "CELERY_BEAT_CLOCKEDSCHEDULE_MODEL"):
from .models.generic import ClockedSchedule

return ClockedSchedule

try:
return apps.get_model(settings.CELERY_BEAT_CLOCKEDSCHEDULE_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_CLOCKEDSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_CLOCKEDSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_CLOCKEDSCHEDULE_MODEL}' that has not "
"been installed"
)
14 changes: 14 additions & 0 deletions django_celery_beat/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .abstract import DAYS, crontab_schedule_celery_timezone
from .generic import (ClockedSchedule, CrontabSchedule, IntervalSchedule,
PeriodicTask, PeriodicTasks, SolarSchedule)

__all__ = [
"ClockedSchedule",
"CrontabSchedule",
"IntervalSchedule",
"PeriodicTask",
"PeriodicTasks",
"SolarSchedule",
"crontab_schedule_celery_timezone",
"DAYS",
]
Loading