22/03/2013, 14:32
|
| | Fecha de Ingreso: julio-2009
Mensajes: 45
Antigüedad: 15 años, 5 meses Puntos: 0 | |
Respuesta: Proximidad Django TEnias razon eso soluciono ese problema , solo que al parecer ahora me dice que el global name distance is not defined segun yo esta definido pongo mi codigo tal como lo tengo
Código:
from django.db import models
# Create your models here.
class lugarManager(models.Manager):
""" location model manager """
def within(self,location,distance):
"""
finds locations within <distance> kilometers of <location>
"""
subquery = 'distance(%(latitude)s,%(longitude)s,latitude,longitude) ' % location.__dict__
condition = 'proximity < %s' % distance
order = 'proximity'
return self.extra(select={'proximity':subquery},where=[condition]).order_by(order)
class Lugar(models.Model):
objects = lugarManager()
nombre = models.CharField(max_length=200)
descripcion = models.CharField(max_length=255)
direccion = models.CharField(max_length=255)
latitud = models.DecimalField(max_digits=12, decimal_places=8)
longitud = models.DecimalField(max_digits=12, decimal_places=8)
def __unicode__(self):
return self.nombre
def distance(p1_lat,p1_long,p2_lat,p2_long):
""" calculates the distance between p1 and p2 """
multiplier = 6371 # for kilometers
#multiplier = 3959 # for miles
return ( multiplier *
acos(
cos( radians(p1_lat) ) *
cos( radians(p2_lat) ) *
cos( radians(p2_long) - radians(p1_long) ) +
sin( radians(p1_lat) ) * sin( radians(p2_lat) )
)
)
from django.db.backends.signals import connection_created
from django.dispatch import receiver
@receiver(connection_created)
def setup_proximity_func(connection,**kwargs):
""" add the proximity function to sqlite """
connection.connection.create_function("distance", 4, distance)
|