I'm not quite sure how to write a test, but I produced a simple script of what goes wrong.
I have a simple Item object which can optionally have a Location
from django.contrib.gis.db import models
class Location(models.Model):
location = models.PointField()
objects = models.GeoManager()
class Item(models.Model):
name = models.CharField(max_length=10)
location = models.ForeignKey(Location, null=True)
objects = models.GeoManager()
Setup some data
from django.contrib.gis.geos import Point
from test.models import Location, Item
item = Item(name="Place 1")
item.save()
item = Item(name="Place 2")
location = Location(location=Point(1,1))
location.save()
item.location = location
item.save()
When I query the Items with select_related() everything works fine
>>> for i in Item.objects.select_related():
... if i.location:
... i.location.location
...
<Point object at 0x21ad784>
>>> print connection.queries[-1]['sql']
SELECT `test_location`.`id`, AsText(`test_location`.`location`) FROM `test_location` WHERE `test_location`.`id` = 1
When I specify select_related('location') to do a LEFT JOIN onto location, the query isn't formed properly
>>> for i in Item.objects.select_related('location'):
... if i.location:
... i.location.location
...
Traceback (most recent call last):
File "<console>", line 3, in <module>
File "/opt/local/lib/python2.5/site-packages/django/contrib/gis/db/models/proxy.py", line 36, in __get__
geom = self._klass(geom_value)
File "/opt/local/lib/python2.5/site-packages/django/contrib/gis/geos/base.py", line 72, in __init__
raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')
ValueError: String or unicode input unrecognized as WKT EWKT, and HEXEWKB.
>>> print connection.queries[-1]['sql']
SELECT `test_item`.`id`, `test_item`.`name`, `test_item`.`location_id`, `test_location`.`id`, `test_location`.`location` FROM `test_item` LEFT OUTER JOIN `test_location` ON (`test_item`.`location_id` = `test_location`.`id`)
I understand that this is a new feature rather than an old one that is broken.