Merge pull request #258 from ownaginatious/constance_dbs_setting

Workaround to make `django-constance` work in multi-DB project
This commit is contained in:
Camilo Nova 2018-03-15 13:49:25 -05:00 committed by GitHub
commit 61356da3fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -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',

View file

@ -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)