summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2010-03-29 22:20:51 +0000
committerJustin Bronn <jbronn@gmail.com>2010-03-29 22:20:51 +0000
commit2613872969f52815859bad9533104b47b63d6375 (patch)
tree912127344a935b9cce15144ef96a647a38ed89e3
parent8ef5daeb4f0e202b0a653379d8d5dedc5f11a9a9 (diff)
[1.1.X] Fixed #10594 -- `GeoQuerySet` measurment methods no longer crash on geometry fields with NULL values. Thanks, whiteinge for the bug report and yourcelf for the initial patch.
Backport of r12885 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12886 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/gis/db/models/sql/query.py6
-rw-r--r--django/contrib/gis/tests/distapp/models.py3
-rw-r--r--django/contrib/gis/tests/distapp/tests.py11
3 files changed, 18 insertions, 2 deletions
diff --git a/django/contrib/gis/db/models/sql/query.py b/django/contrib/gis/db/models/sql/query.py
index 0785bebb13..f4bce309c3 100644
--- a/django/contrib/gis/db/models/sql/query.py
+++ b/django/contrib/gis/db/models/sql/query.py
@@ -245,7 +245,11 @@ class GeoQuery(sql.Query):
# Running through Oracle's first.
value = super(GeoQuery, self).convert_values(value, field or GeomField())
- if isinstance(field, DistanceField):
+ if value is None:
+ # Output from spatial function is NULL (e.g., called
+ # function on a geometry field with NULL value).
+ pass
+ elif isinstance(field, DistanceField):
# Using the field's distance attribute, can instantiate
# `Distance` with the right context.
value = Distance(**{field.distance_att : value})
diff --git a/django/contrib/gis/tests/distapp/models.py b/django/contrib/gis/tests/distapp/models.py
index 4030761578..76e7d3a03f 100644
--- a/django/contrib/gis/tests/distapp/models.py
+++ b/django/contrib/gis/tests/distapp/models.py
@@ -26,11 +26,12 @@ class CensusZipcode(models.Model):
name = models.CharField(max_length=5)
poly = models.PolygonField(srid=4269)
objects = models.GeoManager()
+ def __unicode__(self): return self.name
class SouthTexasZipcode(models.Model):
"Model for a few South Texas ZIP codes."
name = models.CharField(max_length=5)
- poly = models.PolygonField(srid=32140)
+ poly = models.PolygonField(srid=32140, null=True)
objects = models.GeoManager()
def __unicode__(self): return self.name
diff --git a/django/contrib/gis/tests/distapp/tests.py b/django/contrib/gis/tests/distapp/tests.py
index ecc2da2c87..b2c18afbea 100644
--- a/django/contrib/gis/tests/distapp/tests.py
+++ b/django/contrib/gis/tests/distapp/tests.py
@@ -324,6 +324,17 @@ class DistanceTest(unittest.TestCase):
for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')):
self.assertEqual(0, c.perim.m)
+ def test09_measurement_null_fields(self):
+ "Testing the measurement GeoQuerySet methods on fields with NULL values."
+ # Creating SouthTexasZipcode w/NULL value.
+ SouthTexasZipcode.objects.create(name='78212')
+ # Performing distance/area queries against the NULL PolygonField,
+ # and ensuring the result of the operations is None.
+ htown = SouthTexasCity.objects.get(name='Downtown Houston')
+ z = SouthTexasZipcode.objects.distance(htown.point).area().get(name='78212')
+ self.assertEqual(None, z.distance)
+ self.assertEqual(None, z.area)
+
def suite():
s = unittest.TestSuite()
s.addTest(unittest.makeSuite(DistanceTest))