From fdcc0c774a9295f5d0fa3a1edd34e4f7e41476d5 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Mon, 29 Jun 2009 16:31:21 +0000 Subject: [PATCH] Fixed #11381 -- `GeoManager` + `select_related` + nullable `ForeignKey` now works correctly. Thanks, bretthoerner for ticket and dgouldin for initial patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11123 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/db/models/sql/query.py | 2 +- django/contrib/gis/tests/relatedapp/models.py | 2 +- django/contrib/gis/tests/relatedapp/tests.py | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/db/models/sql/query.py b/django/contrib/gis/db/models/sql/query.py index cf1ccf6483..5df15a88b1 100644 --- a/django/contrib/gis/db/models/sql/query.py +++ b/django/contrib/gis/db/models/sql/query.py @@ -225,7 +225,7 @@ class GeoQuery(sql.Query): values.append(self.convert_values(value, field)) else: values.extend(row[index_start:]) - return values + return tuple(values) def convert_values(self, value, field): """ diff --git a/django/contrib/gis/tests/relatedapp/models.py b/django/contrib/gis/tests/relatedapp/models.py index 1125d7fb85..726f9826c0 100644 --- a/django/contrib/gis/tests/relatedapp/models.py +++ b/django/contrib/gis/tests/relatedapp/models.py @@ -40,5 +40,5 @@ class Author(models.Model): class Book(models.Model): title = models.CharField(max_length=100) - author = models.ForeignKey(Author, related_name='books') + author = models.ForeignKey(Author, related_name='books', null=True) objects = models.GeoManager() diff --git a/django/contrib/gis/tests/relatedapp/tests.py b/django/contrib/gis/tests/relatedapp/tests.py index 8c4f83b15a..502a3c0be9 100644 --- a/django/contrib/gis/tests/relatedapp/tests.py +++ b/django/contrib/gis/tests/relatedapp/tests.py @@ -257,6 +257,13 @@ class RelatedGeoModelTest(unittest.TestCase): self.assertEqual(1, len(qs)) self.assertEqual(3, qs[0].num_books) + def test13_select_related_null_fk(self): + "Testing `select_related` on a nullable ForeignKey via `GeoManager`. See #11381." + no_author = Book.objects.create(title='Without Author') + b = Book.objects.select_related('author').get(title='Without Author') + # Should be `None`, and not a 'dummy' model. + self.assertEqual(None, b.author) + # TODO: Related tests for KML, GML, and distance lookups. def suite():