Skip to content

Commit 1db9e74

Browse files
committed
Make app AutoField configurable on first deployment
1 parent 4dd3bdf commit 1db9e74

File tree

8 files changed

+88
-5
lines changed

8 files changed

+88
-5
lines changed

django_celery_results/apps.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
__all__ = ['CeleryResultConfig']
77

8+
from django_celery_results.conf import app_settings
9+
810

911
class CeleryResultConfig(AppConfig):
1012
"""Default configuration for the django_celery_results app."""
1113

1214
name = 'django_celery_results'
1315
label = 'django_celery_results'
1416
verbose_name = _('Celery Results')
15-
default_auto_field = 'django.db.models.AutoField'
17+
default_auto_field = app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD

django_celery_results/conf.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Application settings."""
2+
from __future__ import annotations
3+
4+
from dataclasses import dataclass
5+
from typing import Any
6+
7+
from django.conf import settings as django_settings
8+
9+
# All attributes accessed with this prefix are possible
10+
# to overwrite through django.conf.settings.
11+
SETTINGS_PREFIX = "DJANGO_CELERY_RESULTS_"
12+
13+
14+
@dataclass(frozen=True)
15+
class AppSettings:
16+
"""Proxy class to encapsulate all the app settings.
17+
18+
This instance should be accessed via the singleton
19+
``django_celery_results.conf.app_settings``.
20+
21+
You shouldn't have to set any of these yourself, the class checks a Django
22+
settings with the same name and use these if defined, defaulting to the
23+
values documented here.
24+
"""
25+
26+
DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
27+
28+
def __getattribute__(self, __name: str) -> Any:
29+
"""Check if a Django project settings should override the app default.
30+
31+
In order to avoid returning any random properties of the Django
32+
settings, we first inspect the prefix.
33+
"""
34+
if (
35+
__name.startswith(SETTINGS_PREFIX)
36+
and hasattr(django_settings, __name)
37+
):
38+
return getattr(django_settings, __name)
39+
40+
return super().__getattribute__(__name)
41+
42+
43+
app_settings = AppSettings()

django_celery_results/migrations/0001_initial.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from django.conf import settings
22
from django.db import migrations, models
3+
from django.utils.module_loading import import_string
4+
5+
from django_celery_results.conf import app_settings
6+
7+
auto_field_class = import_string(
8+
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)
39

410

511
class Migration(migrations.Migration):
@@ -13,7 +19,7 @@ class Migration(migrations.Migration):
1319
migrations.CreateModel(
1420
name='TaskResult',
1521
fields=[
16-
('id', models.AutoField(auto_created=True,
22+
('id', auto_field_class(auto_created=True,
1723
primary_key=True,
1824
serialize=False,
1925
verbose_name='ID')),

django_celery_results/migrations/0008_chordcounter.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
from django.conf import settings
44
from django.db import migrations, models
5+
from django.utils.module_loading import import_string
6+
7+
from django_celery_results.conf import app_settings
8+
9+
auto_field_class = import_string(
10+
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)
511

612

713
class Migration(migrations.Migration):
@@ -14,7 +20,7 @@ class Migration(migrations.Migration):
1420
migrations.CreateModel(
1521
name='ChordCounter',
1622
fields=[
17-
('id', models.AutoField(
23+
('id', auto_field_class(
1824
auto_created=True,
1925
primary_key=True,
2026
serialize=False,

django_celery_results/migrations/0009_groupresult.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Generated by Django 3.2 on 2021-04-19 14:55
22
from django.conf import settings
33
from django.db import migrations, models
4+
from django.utils.module_loading import import_string
5+
6+
from django_celery_results.conf import app_settings
7+
8+
auto_field_class = import_string(
9+
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)
410

511

612
class FakeAddIndex(migrations.AddIndex):
@@ -28,7 +34,7 @@ class Migration(migrations.Migration):
2834
migrations.CreateModel(
2935
name='GroupResult',
3036
fields=[
31-
('id', models.AutoField(
37+
('id', auto_field_class(
3238
auto_created=True,
3339
primary_key=True,
3440
serialize=False,

docs/configuration.rst

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Configuration
2+
=============
3+
4+
These are the available settings that can be configured in your Django
5+
project's settings module by defining a setting with the same name.
6+
7+
* ``DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD``: The ``default_auto_field`` used
8+
when first deploying the app, defaults to ``django.db.models.BigAutoField``
9+
if unspecified.
10+
11+
This is only used for the first deployment and
12+
and has no effect if changed after this point. If you want to change its
13+
value after the initial deployment, you should migrate back to zero and
14+
re-apply migrations again::
15+
16+
$ python manage.py migrate django_celery_results zero
17+
$ python manage.py migrate django_celery_results
18+
19+
This will drop and recreate all tables used by django-celery-results.

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Contents
1212

1313
getting_started
1414
injecting_metadata
15+
configuration
1516
copyright
1617

1718
.. toctree::

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ markers =
99
[flake8]
1010
# classes can be lowercase, arguments and variables can be uppercase
1111
# whenever it makes the code more readable.
12-
ignore = N806, N802, N801, N803
12+
ignore = N806, N802, N801, N803, W503
1313

1414
[pep257]
1515
convention=google

0 commit comments

Comments
 (0)