I was initially excited at the idea of specifying a list related
fields with a select_related call, but have since discovered it
doesn't seem to work on reverse relations. I don't know why I was
expecting it to work (stupid optimism!), but it was a little
disappointing. What I was curious about, and I guess this question is
mostly for Malcolm, is how difficult would it be to allow
select_related to follow reverse relations if the fields/models were
specified? If it isn't too difficult, would this be something that
could end up in Django at some point?

To explain what I'm trying to do, I'd like to return unrelated objects
via a shared Model (by using their coordinates) using a single query.
I do have a feeling I probably haven't really thought it through
properly, but at the moment at least, it seems to make sense to
attempt to do it this way.

Here are my models.

# The model that holds Longitude/Latitude coordinates
class GeoPoint(models.Model):
    """ A GeoDjango model that only holds Geographic Coordinates """
    coords = models.PointField(srid=4326)

# The model for a City
class City(models.Model):
    name = models.CharField(max_length=99)
    point  = models.OneToOneField(GeoPoint, related_name='place')

class Landmark(models.Model):
    """ Tourism related landmarks """
    name = models.CharField(max_length=99)
    point  = models.OneToOneField(GeoPoint, related_name='landmark')

class Photo(models.Model):
    """ User uploaded photos """
    name = models.CharField(max_length=99)
    point  = models.OneToOneField(GeoPoint, related_name='photo')

class Cafe(models.Model):
    name = models.CharField(max_length=99)
    point  = models.OneToOneField(GeoPoint, related_name='cafe')

class Event(models.Model):
    name = models.CharField(max_length=99)
    point  = models.OneToOneField(GeoPoint, related_name='event')


With this structure, if we were to visit Los Angeles, we could do the
following to return a queryset of things to see or do when in LA.

# Only display 20 objects
limit = 20

# Get the city of Los Angeles
city = City.objects.get(slug='los-angeles')

# Get the Coordinates of Los Angeles via the
point = city.geopoint.coords

# Find things in Los Angeles to display on a map. This won't work :(
stuff_to_see = GeoPoint.objects.select_related('landmark', 'photo',
'cafe', 'event')

# Sort the found objects by their distance from Los Angeles
stuff_to_see = stuff_to_see.distance(point).order_by('distance')
[:limit]

Now the above won't work as selected_related won't return any objects
as the specified fields don't exist in the GeoPoint model. I guess my
question at this point, as mentioned above, would having
select_related follow reverse relations in this way be too difficult
to implement into the qs-rf branch. I guess my hope is it's simple,
but yeah, things are usually never that simple :)

If it is too difficult/painful to do, then it's probably better I
start thinking of another way to implement this without resorting to
individual queries for each model. Anyone have any suggestions at
other ways to accomplish something similar? Hopefully some of my
example made sense :)

Thanks :)
John



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to