计算纬度长距离ORM查询python django

时间:2018-08-28 05:21:59

标签: python django python-3.x postgresql django-models

Models.py

class Restaurant(models.Model):
    name=models.CharField(max_length=500)
    email=models.CharField(max_length=500)
    web=models.CharField(max_length=500)
    short_description=models.CharField(max_length=500)
    description=models.CharField(max_length=500)
    phone=models.IntegerField(blank=True,null=True,default=0)
    lat =models.FloatField(blank=True,null=True)
    lng =models.FloatField(blank=True,null=True)
    # location = models.PointField(null=False, blank=False, srid=4326, verbose_name="Location")
    address1 = models.CharField(max_length=100)
    address2 = models.CharField(max_length=100,blank=True)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=2)
    postalcode = models.CharField(max_length=20)    
    avg_rating=models.FloatField(blank=True,null=True)
    restaurant_type=models.ForeignKey(RestaurantType,on_delete=models.CASCADE)
    price=models.IntegerField(blank=True,null=True,default=0)
    keywords=models.ManyToManyField(RestaurantKeyword,blank=True)
    def __str__(self):
        return self.name

原始查询:

SELECT id,name,( 3959 * acos ( cos ( radians(" +lat +")) * cos( radians( lat ) ) * cos( radians( lng ) - radians("+lng+") ) + sin ( radians("+lat+") ) * sin( radians( lat ) ) ) ) AS distance  FROM newbackend_restaurant

在此代码中,原始查询工作正常,但我正在Django中寻找ORM查询。

0 个答案:

没有答案