我们的想法是整合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),其值为:
答案 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.