GeoDjango: Can't save a model with PointField(), Error: SpatialProxy (POINT) with value of type: <class 'users.models.Point'>

GeoDjango: Can't save a model with PointField(), Error: SpatialProxy (POINT) with value of type: <class 'users.models.Point'>

想法是集成 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.

但是,当我手动添加经度和纬度后单击保存时,我得到:

TypeError at /admin/users/location/add/ Cannot set Location SpatialProxy (POINT) with value of type:

我相信你用错了Point class。 Point 应该从 django.contrib.gis.geos 而不是 users.models.Point 导入,如果你将它与 PointField

一起使用

您的导入应如下所示:

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

Import Model from Given Code

from django.db import models <br>
from django.contrib.gis.db import models

Then Write Your Code

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.