GeoDjango:无法使用PointField()保存模型,错误:SpatialProxy(POINT),其值类型为:< class 'users.models. Point'>

os8fio9y  于 2023-06-25  发布在  Go
关注(0)|答案(2)|浏览(127)

我们的想法是集成GoogleMap,而不是GeoDjango v1.11.5 PointField()的默认Map。
目前,在我的models.py

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='Teacher')
    placename = models.CharField(blank=True,max_length=255)
    latitude = models.FloatField(blank=True, null=True, verbose_name='Latitude')
    longitude = models.FloatField(blank=True, null=True, verbose_name='Longitude')
    location = models.PointField(blank = True, null=True, srid=4326)
    objects = models.GeoManager()

    def save(self, *args, **kwargs):
        self.location = Point(self.longitude, self.latitude)
        super(Teacher, self).save(*args, **kwargs)  # Call the "real" save() method.

然而,当我手动添加经度和纬度后单击保存时,我得到:
TypeError at/admin/users/location/add/Cannot set Location SpatialProxy(POINT)with value of type:

kiz8lqtg

kiz8lqtg1#

我相信你使用了错误的Point类。Point应该从django.contrib.gis.geos导入,而不是从users.models.Point导入,如果您将其与PointField一起使用
您的导入应如下所示:

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
bpsygsoo

bpsygsoo2#

从给定代码导入模型

from django.db import models
    from django.contrib.gis.db import models

然后编写代码

class Teacher(models.Model):
        user = models.OneToOneField(User, on_delete=models.PROTECT, 
        related_name='Teacher')
        placename = models.CharField(blank=True,max_length=255)
        latitude = models.FloatField(blank=True, null=True, 
        verbose_name='Latitude')
        longitude = models.FloatField(blank=True, null=True, 
        verbose_name='Longitude')
        location = models.PointField(blank = True, null=True, srid=4326)
        objects = models.GeoManager()

        def save(self, *args, **kwargs):
            self.location = Point(self.longitude, self.latitude)
            super(Teacher, self).save(*args, **kwargs)  # Call the "real" 
            save() method.

相关问题