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

独自空忆成欢 提交于 2020-06-27 04:15:21

问题


The idea is to integrate Google Maps instead of the default map for GeoDjango v1.11.5 PointField().

Currently, in my 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.

However, when I click save after I manually add the long and lat, I get:

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


回答1:


I believe you are using the wrong Point class. Point should be imported from django.contrib.gis.geos not users.models.Point if you are using it with a PointField

Your imports should be as follows:

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


来源:https://stackoverflow.com/questions/46102348/geodjango-cant-save-a-model-with-pointfield-error-spatialproxy-point-wit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!