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

如何从django mapbox LocationField检索和保存纬度和经度?,django,django-models,mapbox,geodjango,Django,Django Models,Mapbox,Geodjango,我使用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,

我使用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)

我想保存纬度和经度,但出现了以下错误:“tuple”对象没有属性“y”

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

如果要使用这些函数,可以从
django.contrib.gis.geos
目录中的
point.py
linestring.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