Skip to content

Commit 2613872

Browse files
committed
[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
1 parent 8ef5dae commit 2613872

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

django/contrib/gis/db/models/sql/query.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ def convert_values(self, value, field):
245245
# Running through Oracle's first.
246246
value = super(GeoQuery, self).convert_values(value, field or GeomField())
247247

248-
if isinstance(field, DistanceField):
248+
if value is None:
249+
# Output from spatial function is NULL (e.g., called
250+
# function on a geometry field with NULL value).
251+
pass
252+
elif isinstance(field, DistanceField):
249253
# Using the field's distance attribute, can instantiate
250254
# `Distance` with the right context.
251255
value = Distance(**{field.distance_att : value})

django/contrib/gis/tests/distapp/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ class CensusZipcode(models.Model):
2626
name = models.CharField(max_length=5)
2727
poly = models.PolygonField(srid=4269)
2828
objects = models.GeoManager()
29+
def __unicode__(self): return self.name
2930

3031
class SouthTexasZipcode(models.Model):
3132
"Model for a few South Texas ZIP codes."
3233
name = models.CharField(max_length=5)
33-
poly = models.PolygonField(srid=32140)
34+
poly = models.PolygonField(srid=32140, null=True)
3435
objects = models.GeoManager()
3536
def __unicode__(self): return self.name
3637

django/contrib/gis/tests/distapp/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,17 @@ def test08_perimeter(self):
324324
for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')):
325325
self.assertEqual(0, c.perim.m)
326326

327+
def test09_measurement_null_fields(self):
328+
"Testing the measurement GeoQuerySet methods on fields with NULL values."
329+
# Creating SouthTexasZipcode w/NULL value.
330+
SouthTexasZipcode.objects.create(name='78212')
331+
# Performing distance/area queries against the NULL PolygonField,
332+
# and ensuring the result of the operations is None.
333+
htown = SouthTexasCity.objects.get(name='Downtown Houston')
334+
z = SouthTexasZipcode.objects.distance(htown.point).area().get(name='78212')
335+
self.assertEqual(None, z.distance)
336+
self.assertEqual(None, z.area)
337+
327338
def suite():
328339
s = unittest.TestSuite()
329340
s.addTest(unittest.makeSuite(DistanceTest))

0 commit comments

Comments
 (0)