Custom Database Engine for django-tenants and postgis

Hi, I am getting stuck with django-tenants adding new tenants. I set it up and it was working great. Then I had to bring in postgis to be able to integrate with Google Maps and that also worked great. But now going back to adding a tenant, that part of the functionality doesnt work. I have tried everything but cant seem to get it back to work without breaking the postGIS bit. Any help? Here is the relevant bits:

DATABASES in settings.py
‘ENGINE’: ‘project.tenant_postgis’,

the tenant_postgis.py file

project/tenant_postgis/base.py

from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper as PostGISDatabaseWrapper

from django_tenants.utils import get_public_schema_name

class DatabaseWrapper(PostGISDatabaseWrapper):

def \__init_\_(self, \*args, \*\*kwargs):

    super().\__init_\_(\*args, \*\*kwargs)

    self.schema_name = None

    self.tenant = None



def set_schema(self, schema_name, tenant_type=None, include_public=True):

    self.schema_name = schema_name or get_public_schema_name()

    search_path = f'"{self.schema_name}"'

    if include_public:

        search_path += ', public'

    cursor = self.cursor()

    cursor.execute(f'SET search_path TO {search_path}')

    cursor.close()



def set_schema_to_public(self):

    self.set_schema(get_public_schema_name())

    \# Reset tenant attributes on BOTH wrapper and connection

    self.tenant = None

    self.connection.tenant = None if self.connection else None



def set_tenant(self, tenant):

    if tenant is None:

        self.set_schema_to_public()

        return



    schema_name = getattr(tenant, "schema_name", str(tenant))

    self.set_schema(schema_name)

    \# Set tenant on BOTH wrapper and connection

    self.tenant = tenant

    self.connection.tenant = tenant if self.connection else None



def get_schema(self):

    return self.schema_name or get_public_schema_name()



def init_connection_state(self):

    super().init_connection_state()

    try:

        if self.connection and not getattr(self.connection, "closed", False):

            self.set_schema_to_public()

    except Exception:

        pass

What exactly do you mean by this? Any error?

Most probably, PostGIS connection is not fully configured before the tenant creation process begins.