From a91e59e4a0fa9cbc6c662feaad6ac17b0f07ce9a Mon Sep 17 00:00:00 2001 From: David Buhler Date: Mon, 20 Feb 2023 12:31:03 -0700 Subject: [PATCH] Fixed #34344 -- Allowed PostGIS DatabaseWrapper subclasses to customize (features/introspection/ops)_class classes. --- .../contrib/gis/db/backends/postgis/base.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/django/contrib/gis/db/backends/postgis/base.py b/django/contrib/gis/db/backends/postgis/base.py index 23ec0553f82..8360c0e19a1 100644 --- a/django/contrib/gis/db/backends/postgis/base.py +++ b/django/contrib/gis/db/backends/postgis/base.py @@ -2,6 +2,15 @@ from functools import lru_cache from django.db.backends.base.base import NO_DB_ALIAS from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper +from django.db.backends.postgresql.features import ( + DatabaseFeatures as PsycopgDatabaseFeatures, +) +from django.db.backends.postgresql.introspection import ( + DatabaseIntrospection as PsycopgDatabaseIntrospection, +) +from django.db.backends.postgresql.operations import ( + DatabaseOperations as PsycopgDatabaseOperations, +) from django.db.backends.postgresql.psycopg_any import is_psycopg3 from .adapter import PostGISAdapter @@ -81,6 +90,9 @@ if is_psycopg3: class DatabaseWrapper(PsycopgDatabaseWrapper): SchemaEditorClass = PostGISSchemaEditor + features_class = DatabaseFeatures + ops_class = PostGISOperations + introspection_class = PostGISIntrospection _type_infos = { "geometry": {}, @@ -89,11 +101,13 @@ class DatabaseWrapper(PsycopgDatabaseWrapper): } def __init__(self, *args, **kwargs): + if kwargs.get("alias", "") == NO_DB_ALIAS: + # Don't initialize PostGIS-specific stuff for non-db connections. + self.features_class = PsycopgDatabaseFeatures + self.ops_class = PsycopgDatabaseOperations + self.introspection_class = PsycopgDatabaseIntrospection + super().__init__(*args, **kwargs) - if kwargs.get("alias", "") != NO_DB_ALIAS: - self.features = DatabaseFeatures(self) - self.ops = PostGISOperations(self) - self.introspection = PostGISIntrospection(self) def prepare_database(self): super().prepare_database()