From 174cf83b2b4f6501e831ba2f27bf5e57447aff44 Mon Sep 17 00:00:00 2001 From: landonsilla Date: Wed, 17 Aug 2022 12:30:41 -0700 Subject: [PATCH 1/2] delaying import of ContentType until django is fully loaded --- tenant_schemas/postgresql_backend/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index 212d3eae..42795ab2 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -3,7 +3,6 @@ import psycopg2 from django.conf import settings -from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured, ValidationError import django.db.utils @@ -76,6 +75,7 @@ def set_schema(self, schema_name, include_public=True): Main API method to current database schema, but it does not actually modify the db connection. """ + is_init = not has_attr(self, 'schema_name') self.tenant = FakeTenant(schema_name=schema_name) self.schema_name = schema_name self.include_public_schema = include_public @@ -88,7 +88,9 @@ def set_schema(self, schema_name, include_public=True): # on public, a particular model has id 14, but on the tenants it has # the id 15. if 14 is cached instead of 15, the permissions for the # wrong model will be fetched. - ContentType.objects.clear_cache() + if not is_init: + from django.contrib.contenttypes.models import ContentType + ContentType.objects.clear_cache() def set_schema_to_public(self): """ From e5c01f223ed05910e00fd02ed3cc03539042dbde Mon Sep 17 00:00:00 2001 From: landonsilla Date: Wed, 17 Aug 2022 14:22:04 -0700 Subject: [PATCH 2/2] small fix --- tenant_schemas/postgresql_backend/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index 42795ab2..a39d422d 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -75,7 +75,7 @@ def set_schema(self, schema_name, include_public=True): Main API method to current database schema, but it does not actually modify the db connection. """ - is_init = not has_attr(self, 'schema_name') + is_init = not hasattr(self, 'schema_name') self.tenant = FakeTenant(schema_name=schema_name) self.schema_name = schema_name self.include_public_schema = include_public