Skip to content

Commit 9bd5e55

Browse files
committed
Avoid some boring migrations
1 parent f988e0b commit 9bd5e55

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Change log
88

99
.. _Next version: https://github.com/feincms/feincms/compare/v22.3.0...main
1010

11+
- Changed the ``template_key`` field type to avoid boring migrations because of
12+
changing choices.
13+
1114

1215
`v22.3.0`_ (2022-05-17)
1316
~~~~~~~~~~~~~~~~~~~~~~~

feincms/models.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from django.utils.translation import gettext_lazy as _
2222

2323
from feincms.extensions import ExtensionsMixin
24-
from feincms.utils import copy_model_instance
24+
from feincms.utils import ChoicesCharField, copy_model_instance
2525

2626

2727
class Region:
@@ -394,14 +394,9 @@ def register_templates(cls, *templates):
394394
except (StopIteration,):
395395
cls.add_to_class(
396396
"template_key",
397-
models.CharField(
397+
ChoicesCharField(
398398
_("template"),
399399
max_length=255,
400-
choices=(
401-
# Dummy choice to trick Django. Cannot be empty,
402-
# otherwise admin.E023 happens.
403-
("__dummy", "__dummy"),
404-
),
405400
),
406401
)
407402
field = next(

feincms/utils/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from django.apps import apps
99
from django.core.exceptions import ImproperlyConfigured
10-
from django.db.models import AutoField
10+
from django.db.models import AutoField, CharField
1111

1212
from feincms import settings
1313

@@ -151,3 +151,19 @@ def get_singleton(template_key, cls=None, raise_exception=True):
151151
def get_singleton_url(template_key, cls=None, raise_exception=True):
152152
obj = get_singleton(template_key, cls, raise_exception)
153153
return obj.get_absolute_url() if obj else "#broken-link"
154+
155+
156+
class ChoicesCharField(CharField):
157+
"""
158+
``models.CharField`` with choices, which makes the migration framework
159+
always ignore changes to ``choices``, ever.
160+
"""
161+
162+
def __init__(self, *args, **kwargs):
163+
kwargs.setdefault("choices", [("", "")]) # Non-empty choices for get_*_display
164+
super().__init__(*args, **kwargs)
165+
166+
def deconstruct(self):
167+
name, path, args, kwargs = super().deconstruct()
168+
kwargs["choices"] = [("", "")]
169+
return name, "django.db.models.CharField", args, kwargs

0 commit comments

Comments
 (0)