djangogeodjango

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


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:


Solution

  • 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