I have a model (Sale) that has latitude and longitude fields. I'm
trying to take one Sale object and find others within a certain radius
(using the great circle formula). When I do this using the extra()
QuerySet modifier, it returns a queryset with all the Sale objects in
the database. But when I manually execute the raw SQL query that's
being run (according to connection.queries), it returns the result
that I'm expecting.

I'm using version 0.97-pre-SVN-7121 with MySQL.

Am I doing something wrong?

>From my shell session:

>>> from data.models import Sale
>>> from django.db import connection
>>>
>>> this_sale = Sale.objects.order_by('?')[0]
>>> distance_query = "acos( SIN( PI( ) * %s /180 ) * SIN( PI( ) * 
>>> `data_sale`.`latitude` /180 ) + COS( PI( ) * %s /180 ) * COS( PI( ) * 
>>> `data_sale`.`latitude` /180 ) * COS( PI( ) * `data_sale`.`longitude` /180 - 
>>> PI( ) * %s /180 ) ) * 3963.191" % (this_sale.latitude, this_sale.latitude, 
>>> this_sale.longitude)
>>>
>>> nearby_sales = Sale.objects.extra(where=['%s < %s'], 
>>> params=[distance_query, .5])
>>> nearby_sales.count()
52448L
>>>
>>> query = connection.queries[-1]['sql']
>>> query
u'SELECT COUNT(*) FROM `data_sale` WHERE acos( SIN( PI( ) * 37.41915 /
180 ) * SIN( PI( ) * `data_sale`.`latitude` /180 ) + COS( PI( ) *
37.41915 /180 ) * COS( PI( ) * `data_sale`.`latitude` /180 ) *
COS( PI( ) * `data_sale`.`longitude` /180 - PI( ) * -76.514234 /
180 ) ) * 3963.191 < 0.5'
>>> cursor = connection.cursor()
>>> cursor.execute(query)
1L
>>> cursor.fetchone()[0]
34L
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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