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 #10594 -- `GeoQuerySet` measurment methods no longer cr…

…ash 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
  • Loading branch information...
commit 2613872969f52815859bad9533104b47b63d6375 1 parent 8ef5dae
Justin Bronn authored March 29, 2010
6  django/contrib/gis/db/models/sql/query.py
@@ -245,7 +245,11 @@ def convert_values(self, value, field):
245 245
             # Running through Oracle's first.
246 246
             value = super(GeoQuery, self).convert_values(value, field or GeomField())
247 247
 
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):
249 253
             # Using the field's distance attribute, can instantiate
250 254
             # `Distance` with the right context.
251 255
             value = Distance(**{field.distance_att : value})
3  django/contrib/gis/tests/distapp/models.py
@@ -26,11 +26,12 @@ class CensusZipcode(models.Model):
26 26
     name = models.CharField(max_length=5)
27 27
     poly = models.PolygonField(srid=4269)
28 28
     objects = models.GeoManager()
  29
+    def __unicode__(self): return self.name
29 30
 
30 31
 class SouthTexasZipcode(models.Model):
31 32
     "Model for a few South Texas ZIP codes."
32 33
     name = models.CharField(max_length=5)
33  
-    poly = models.PolygonField(srid=32140)
  34
+    poly = models.PolygonField(srid=32140, null=True)
34 35
     objects = models.GeoManager()
35 36
     def __unicode__(self): return self.name
36 37
 
11  django/contrib/gis/tests/distapp/tests.py
@@ -324,6 +324,17 @@ def test08_perimeter(self):
324 324
         for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')):
325 325
             self.assertEqual(0, c.perim.m)
326 326
 
  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
+
327 338
 def suite():
328 339
     s = unittest.TestSuite()
329 340
     s.addTest(unittest.makeSuite(DistanceTest))

0 notes on commit 2613872

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