diff --git a/constance/apps.py b/constance/apps.py index 6eb988e..1de2ab1 100644 --- a/constance/apps.py +++ b/constance/apps.py @@ -16,9 +16,13 @@ class ConstanceConfig(AppConfig): Creates a fake content type and permission to be able to check for permissions """ + from django.conf import settings from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType + constance_dbs = getattr(settings, 'CONSTANCE_DBS', None) + if constance_dbs is not None and using not in constance_dbs: + return if ContentType._meta.installed and Permission._meta.installed: content_type, created = ContentType.objects.using(using).get_or_create( app_label='constance', diff --git a/tests/test_app.py b/tests/test_app.py index b4c38c6..5ce70a6 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -2,7 +2,7 @@ from django.apps import apps from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType from django.db.models import signals -from django.test import TestCase +from django.test import TestCase, override_settings class TestApp(TestCase): @@ -20,6 +20,14 @@ class TestApp(TestCase): self.assert_content_type_and_permission_created('default') + @override_settings(CONSTANCE_DBS=['default']) + def test_only_use_databases_in_constance_dbs(self): + Permission.objects.using('default').delete() + Permission.objects.using('secondary').delete() + self.assert_uses_correct_database('default') + with self.assertRaises(AssertionError): + self.assert_uses_correct_database('secondary') + def assert_uses_correct_database(self, database_name): self.call_post_migrate(database_name)