Django模型比较不适用于float数据类型

时间:2018-09-04 07:13:35

标签: python django model geolocation

我正在从事一个需要定位服务的项目。我正在使用Google的地理编码(反向)api从原始坐标(纬度和经度)(例如城市,省,县等)获取详细信息 我的地址解析是在服务器端完成的,这使我能够缓存结果,从而避免了来自Google的查询限制。 我的缓存表在位置范围内(西北和东南坐标)工作。 我首先在PHP和MySql中实现了此功能,并且运行良好。

#this is the sql query to search for location within a specific bound
SELECT _mainid FROM {$this->addressCacheTable} WHERE $lat<= _nlat AND $lat>=_slat AND $lon>=_slong AND $lon<=_nlong

我正在尝试使用django框架切换到python。我有这个

#this is my query statement in django 
data = Coordinates.objects.filter(nlat__lte=lat, slat__gte=lat, nlon__gte=lon, slon__lte=lon)

在python中,即使它们是逻辑上与我的查询匹配的值,我也总是得到空结果。我如何使django代码像我的PHP / MySql一样工作

请问我现在已经停滞了好几天,任何朝着正确方向的指针将最有帮助。另外,如果我的实现存在任何问题,或者有更好的解决方法,我将不胜感激。

#Database structure with PHP and MySql

CREATE TABLE "address_cache" ( "id" serial NOT NULL REFERENCES "location_table" ("id"), "_nlat" DECIMAL(8,6) NOT NULL, "_nlon" DECIMAL(9,6) NOT NULL, "_slat" DECIMAL(8,6) NOT NULL, "_slong" DECIMAL(9,6) NOT NULL, );

#django Model for coordinates using django-mptt to model hierarchical relationship between country,state,county ......etc

    class LocationInfo(MPTTModel):
        text = models.CharField(max_length=MAX_CHAR_LENGTH, unique=True)
        parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

        class MPTTMeta:
            order_insertion_by = ['text']

        def __str__(self):
            return self.text

    class Coordinates(MPTTModel):
        nlat =models.FloatField(default=0.0)
        slat =models.FloatField(default=0.0)
        nlon = models.FloatField(default=0.0)
        slon = models.FloatField(default=0.0)
        location=TreeForeignKey(LocationInfo,on_delete=models.CASCADE,related_name='coordinates',to_field='id')
        parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

        class MPTTMeta:
            order_insertion_by = ['location']

        def __str__(self):
            return "Bounds {NE:%f,%f SW:%f,%f} Location: %s"%(self.nlat,self.nlon,self.slat,self.slon,self.location.text)

0 个答案:

没有答案