Ver Mensaje Individual
  #5 (permalink)  
Antiguo 22/03/2013, 12:41
DankarSilob
 
Fecha de Ingreso: julio-2009
Mensajes: 45
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Proximidad Django

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
eso tego ne mis models pero no me hallo donde poner
Código:
from math import sin, cos, acos, radians
 
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) )
    		)
    	)
ni tampoco

Código:
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)