如何从django mapbox LocationField中检索和保存纬度和经度



我用mapbox locationfield:护理一个django模型

from mapbox_location_field.models import LocationField
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.SET_NULL ,blank=True,null=True)
location = LocationField(null=True, blank=True)
latitude = models.DecimalField(max_digits=20,decimal_places=18,null=True, blank=True)
longitude = models.DecimalField(max_digits=20,decimal_places=18,null=True, blank=True)
def __str__(self):
return self.store_name
def save(self,*args, **kwargs):
if self.location:
self.latitude = self.location.y
self.longitude = self.location.x
super(Profile,self).save(*args, **kwargs)

我想保存纬度和经度,但我得到了这个错误:"元组"对象没有属性"y">

指定的.x/.y函数是从mapbox_location_field.models导入的,但您无法运行代码,因为这些函数不在模型文件中。所以LocationField类没有您想要的函数。您可以在此处的链接中查看源代码。

如果要使用这些函数,可以从django.contrib.gis.geos目录中的point.pylinestring.py等文件夹访问它们。有关更多详细信息,请参阅源代码。

你可以试试这个:

from django.db import models
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
class Location(models.Model):
location = models.PointField(blank = True, null=True)

def longitude(self):
return self.location.x
def latitude(self):
return self.location.y

最新更新