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

时间:2017-09-07 17:45:52

标签: django geodjango

我们的想法是整合Google地图而不是GeoDjango v1.11.5 PointField()的默认地图。

目前,在我的 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.

但是,当我手动添加long和lat之后单击“保存”时,我得到:

  

/ admin / users / location / add /中的TypeError无法设置位置   SpatialProxy(POINT),其值为:

2 个答案:

答案 0 :(得分:3)

我相信您使用的是错误的Point课程。 Point如果您使用django.contrib.gis.geos

,则应从users.models.Point而不是PointField导入from django.contrib.gis.db import models from django.contrib.gis.geos import Point

您的导入应如下所示:

EC_KEY

答案 1 :(得分:0)

<块引用>

从给定代码导入模型

from django.db import models <br>
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.