Commits

Andrew Godwin committed d0e7724

Fix geodhjjango detection

Comments (0)

Files changed (1)

south/introspection_plugins/geodjango.py

           ((settings.DATABASE_ENGINE in ["postgresql", "postgresql_psycopg2", "mysql"]) or \
           (settings.DATABASE_ENGINE == "sqlite3" and has_spatialite()))
 
+# Build a tuple of possible database errors
+database_error_classes = tuple()
+try:
+    from psycopg2 import ProgrammingError
+except ImportError:
+    pass
+else:
+    database_error_classes += (ProgrammingError,)
+
 if has_gis:
     # Alright,import the field
-    from django.contrib.gis.db.models.fields import GeometryField
-    
-    # Make some introspection rules
-    if django.VERSION[0] == 1 and django.VERSION[1] >= 1:
-        # Django 1.1's gis module renamed these.
-        rules = [
-            (
-                (GeometryField, ),
-                [],
-                {
-                    "srid": ["srid", {"default": 4326}],
-                    "spatial_index": ["spatial_index", {"default": True}],
-                    "dim": ["dim", {"default": 2}],
-                },
-            ),
-        ]
+    try:
+        from django.contrib.gis.db.models.fields import GeometryField
+    except database_error_classes:
+        has_gis = False
     else:
-        rules = [
-            (
-                (GeometryField, ),
-                [],
-                {
-                    "srid": ["_srid", {"default": 4326}],
-                    "spatial_index": ["_spatial_index", {"default": True}],
-                    "dim": ["_dim", {"default": 2}],
-                },
-            ),
-        ]
-    
-    # Install them
-    add_introspection_rules(rules, ["^django\.contrib\.gis"])
+        # Make some introspection rules
+        if django.VERSION[0] == 1 and django.VERSION[1] >= 1:
+            # Django 1.1's gis module renamed these.
+            rules = [
+                (
+                    (GeometryField, ),
+                    [],
+                    {
+                        "srid": ["srid", {"default": 4326}],
+                        "spatial_index": ["spatial_index", {"default": True}],
+                        "dim": ["dim", {"default": 2}],
+                    },
+                ),
+            ]
+        else:
+            rules = [
+                (
+                    (GeometryField, ),
+                    [],
+                    {
+                        "srid": ["_srid", {"default": 4326}],
+                        "spatial_index": ["_spatial_index", {"default": True}],
+                        "dim": ["_dim", {"default": 2}],
+                    },
+                ),
+            ]
+        
+        # Install them
+        add_introspection_rules(rules, ["^django\.contrib\.gis"])