diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 73d943d26e8..cf91ce83d4d 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -30,6 +30,18 @@ FILE_CHARSET_DEPRECATED_MSG = ( ) +class SettingsReference(str): + """ + String subclass which references a current settings value. It's treated as + the value in memory but serializes to a settings.NAME attribute reference. + """ + def __new__(self, value, setting_name): + return str.__new__(self, value) + + def __init__(self, value, setting_name): + self.setting_name = setting_name + + class LazySettings(LazyObject): """ A lazy proxy for either global Django settings or a custom settings object. diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index 6fe49e4a8a9..d395313ff47 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -9,6 +9,7 @@ import re import types import uuid +from django.conf import SettingsReference from django.db import models from django.db.migrations.operations.base import Operation from django.db.migrations.utils import COMPILED_REGEX_TYPE, RegexObject @@ -271,7 +272,6 @@ class UUIDSerializer(BaseSerializer): def serializer_factory(value): - from django.db.migrations.writer import SettingsReference if isinstance(value, Promise): value = str(value) elif isinstance(value, LazyObject): diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 6a62b4cbf4f..1e001da4e6d 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -4,6 +4,8 @@ from importlib import import_module from django import get_version from django.apps import apps +# SettingsReference imported for backwards compatibility in Django 2.2. +from django.conf import SettingsReference # NOQA from django.db import migrations from django.db.migrations.loader import MigrationLoader from django.db.migrations.serializer import serializer_factory @@ -12,20 +14,6 @@ from django.utils.module_loading import module_dir from django.utils.timezone import now -class SettingsReference(str): - """ - Special subclass of string which actually references a current settings - value. It's treated as the value in memory, but serializes out to a - settings.NAME attribute reference. - """ - - def __new__(self, value, setting_name): - return str.__new__(self, value) - - def __init__(self, value, setting_name): - self.setting_name = setting_name - - class OperationWriter: def __init__(self, operation, indentation=2): self.operation = operation diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 2c30a157004..339e4ec7679 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -4,6 +4,7 @@ from functools import partial from django import forms from django.apps import apps +from django.conf import SettingsReference from django.core import checks, exceptions from django.db import connection, router from django.db.backends import utils @@ -590,7 +591,6 @@ class ForeignObject(RelatedField): % (kwargs['to'].setting_name, swappable_setting) ) # Set it - from django.db.migrations.writer import SettingsReference kwargs['to'] = SettingsReference( kwargs['to'], swappable_setting, @@ -1457,7 +1457,6 @@ class ManyToManyField(RelatedField): "(%s and %s)" % (kwargs['to'].setting_name, swappable_setting) ) - from django.db.migrations.writer import SettingsReference kwargs['to'] = SettingsReference( kwargs['to'], swappable_setting, diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 467ff4475b2..8e30342763c 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -12,12 +12,10 @@ import custom_migration_operations.more_operations import custom_migration_operations.operations from django import get_version -from django.conf import settings +from django.conf import SettingsReference, settings from django.core.validators import EmailValidator, RegexValidator from django.db import migrations, models -from django.db.migrations.writer import ( - MigrationWriter, OperationWriter, SettingsReference, -) +from django.db.migrations.writer import MigrationWriter, OperationWriter from django.test import SimpleTestCase from django.utils.deconstruct import deconstructible from django.utils.functional import SimpleLazyObject