Skip to content

Commit

Permalink
[1.1.X] Fixed #12093 -- LayerMapping now takes into account model i…
Browse files Browse the repository at this point in the history
…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
jbronn committed Nov 2, 2009
1 parent e114f44 commit 30ebd26
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions django/contrib/gis/tests/geoapp/test_regress.py
Expand Up @@ -28,6 +28,7 @@ def test02_kmz(self):
kmz = render_to_kmz('gis/kml/placemarks.kml', {'places' : places})

@no_spatialite
@no_mysql
def test03_extent(self):
"Testing `extent` on a table with a single point, see #11827."
pnt = City.objects.get(name='Pueblo').point
Expand Down
14 changes: 14 additions & 0 deletions django/contrib/gis/tests/layermap/models.py
Expand Up @@ -29,6 +29,20 @@ class Interstate(models.Model):
path = models.LineStringField()
objects = models.GeoManager()

# Same as `City` above, but for testing model inheritance.
class CityBase(models.Model):
name = models.CharField(max_length=25)
population = models.IntegerField()
density = models.DecimalField(max_digits=7, decimal_places=1)
point = models.PointField()
objects = models.GeoManager()

class ICity1(CityBase):
dt = models.DateField()

class ICity2(ICity1):
dt_time = models.DateTimeField(auto_now=True)

# Mapping dictionaries for the models above.
co_mapping = {'name' : 'Name',
'state' : {'name' : 'State'}, # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
Expand Down
22 changes: 21 additions & 1 deletion django/contrib/gis/tests/layermap/tests.py
@@ -1,7 +1,7 @@
import os, unittest
from copy import copy
from decimal import Decimal
from models import City, County, CountyFeat, Interstate, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
from models import City, County, CountyFeat, Interstate, ICity1, ICity2, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
from django.contrib.gis.db.backend import SpatialBackend
from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
from django.contrib.gis.gdal import DataSource
Expand Down Expand Up @@ -242,6 +242,26 @@ def clear_counties(): County.objects.all().delete()
lm.save(step=st, strict=True)
self.county_helper(county_feat=False)

def test06_model_inheritance(self):
"Tests LayerMapping on inherited models. See #12093."
icity_mapping = {'name' : 'Name',
'population' : 'Population',
'density' : 'Density',
'point' : 'POINT',
'dt' : 'Created',
}

# Parent model has geometry field.
lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
lm1.save()

# Grandparent has geometry field.
lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
lm2.save()

self.assertEqual(6, ICity1.objects.count())
self.assertEqual(3, ICity2.objects.count())

def suite():
s = unittest.TestSuite()
s.addTest(unittest.makeSuite(LayerMapTest))
Expand Down
20 changes: 15 additions & 5 deletions django/contrib/gis/utils/layermapping.py
Expand Up @@ -514,16 +514,26 @@ def coord_transform(self):
def geometry_column(self):
"Returns the GeometryColumn model associated with the geographic column."
from django.contrib.gis.models import GeometryColumns
# Getting the GeometryColumn object.
# Use the `get_field_by_name` on the model's options so that we
# get the correct model if there's model inheritance -- otherwise
# the returned model is None.
opts = self.model._meta
fld, model, direct, m2m = opts.get_field_by_name(self.geom_field)
if model is None: model = self.model

# Trying to get the `GeometryColumns` object that corresponds to the
# the geometry field.
try:
db_table = self.model._meta.db_table
geo_col = self.geom_field
db_table = model._meta.db_table
geo_col = fld.column

if SpatialBackend.oracle:
# Making upper case for Oracle.
db_table = db_table.upper()
geo_col = geo_col.upper()
gc_kwargs = {GeometryColumns.table_name_col() : db_table,
GeometryColumns.geom_col_name() : geo_col,

gc_kwargs = { GeometryColumns.table_name_col() : db_table,
GeometryColumns.geom_col_name() : geo_col,
}
return GeometryColumns.objects.get(**gc_kwargs)
except Exception, msg:
Expand Down

0 comments on commit 30ebd26

Please sign in to comment.