Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12093 -- `LayerMapping` now takes into account model i…

…nheritance when looking for the geometry column; forgot to diable extent regression test on MySQL.

Backport of r11703 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11704 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 30ebd268be0f960082c7d721d911d699f67dd0a9 1 parent e114f44
Justin Bronn authored November 02, 2009
1  django/contrib/gis/tests/geoapp/test_regress.py
@@ -28,6 +28,7 @@ def test02_kmz(self):
28 28
         kmz = render_to_kmz('gis/kml/placemarks.kml', {'places' : places})
29 29
 
30 30
     @no_spatialite
  31
+    @no_mysql
31 32
     def test03_extent(self):
32 33
         "Testing `extent` on a table with a single point, see #11827."
33 34
         pnt = City.objects.get(name='Pueblo').point
14  django/contrib/gis/tests/layermap/models.py
@@ -29,6 +29,20 @@ class Interstate(models.Model):
29 29
     path = models.LineStringField()
30 30
     objects = models.GeoManager()
31 31
 
  32
+# Same as `City` above, but for testing model inheritance.
  33
+class CityBase(models.Model):
  34
+    name = models.CharField(max_length=25)
  35
+    population = models.IntegerField()
  36
+    density = models.DecimalField(max_digits=7, decimal_places=1)
  37
+    point = models.PointField()
  38
+    objects = models.GeoManager()
  39
+
  40
+class ICity1(CityBase):
  41
+    dt = models.DateField()
  42
+    
  43
+class ICity2(ICity1):
  44
+    dt_time = models.DateTimeField(auto_now=True)
  45
+
32 46
 # Mapping dictionaries for the models above.
33 47
 co_mapping = {'name' : 'Name',
34 48
               'state' : {'name' : 'State'}, # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
22  django/contrib/gis/tests/layermap/tests.py
... ...
@@ -1,7 +1,7 @@
1 1
 import os, unittest
2 2
 from copy import copy
3 3
 from decimal import Decimal
4  
-from models import City, County, CountyFeat, Interstate, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
  4
+from models import City, County, CountyFeat, Interstate, ICity1, ICity2, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
5 5
 from django.contrib.gis.db.backend import SpatialBackend
6 6
 from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
7 7
 from django.contrib.gis.gdal import DataSource
@@ -242,6 +242,26 @@ def clear_counties(): County.objects.all().delete()
242 242
             lm.save(step=st, strict=True)
243 243
             self.county_helper(county_feat=False)
244 244
 
  245
+    def test06_model_inheritance(self):
  246
+        "Tests LayerMapping on inherited models.  See #12093."
  247
+        icity_mapping = {'name' : 'Name',
  248
+                         'population' : 'Population',
  249
+                         'density' : 'Density',
  250
+                         'point' : 'POINT',
  251
+                         'dt' : 'Created',
  252
+                         }
  253
+
  254
+        # Parent model has geometry field.
  255
+        lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
  256
+        lm1.save()
  257
+
  258
+        # Grandparent has geometry field.
  259
+        lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
  260
+        lm2.save()
  261
+
  262
+        self.assertEqual(6, ICity1.objects.count())
  263
+        self.assertEqual(3, ICity2.objects.count())
  264
+        
245 265
 def suite():
246 266
     s = unittest.TestSuite()
247 267
     s.addTest(unittest.makeSuite(LayerMapTest))
20  django/contrib/gis/utils/layermapping.py
@@ -514,16 +514,26 @@ def coord_transform(self):
514 514
     def geometry_column(self):
515 515
         "Returns the GeometryColumn model associated with the geographic column."
516 516
         from django.contrib.gis.models import GeometryColumns
517  
-        # Getting the GeometryColumn object.
  517
+        # Use the `get_field_by_name` on the model's options so that we
  518
+        # get the correct model if there's model inheritance -- otherwise
  519
+        # the returned model is None.
  520
+        opts = self.model._meta
  521
+        fld, model, direct, m2m = opts.get_field_by_name(self.geom_field)
  522
+        if model is None: model = self.model
  523
+
  524
+        # Trying to get the `GeometryColumns` object that corresponds to the
  525
+        # the geometry field.
518 526
         try:
519  
-            db_table = self.model._meta.db_table
520  
-            geo_col = self.geom_field
  527
+            db_table = model._meta.db_table
  528
+            geo_col = fld.column
  529
+
521 530
             if SpatialBackend.oracle:
522 531
                 # Making upper case for Oracle.
523 532
                 db_table = db_table.upper()
524 533
                 geo_col = geo_col.upper()
525  
-            gc_kwargs = {GeometryColumns.table_name_col() : db_table,
526  
-                         GeometryColumns.geom_col_name() : geo_col,
  534
+
  535
+            gc_kwargs = { GeometryColumns.table_name_col() : db_table,
  536
+                          GeometryColumns.geom_col_name() : geo_col,
527 537
                          }
528 538
             return GeometryColumns.objects.get(**gc_kwargs)
529 539
         except Exception, msg:

0 notes on commit 30ebd26

Please sign in to comment.
Something went wrong with that request. Please try again.