7

I am working with GeoDjango for the first time and am having trouble adding a PointField to my model. Here is my model code:

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

class Crime(models.Model):
    Category = models.CharField(max_length=50)
    Description = models.CharField(max_length=150)
    District =models.CharField(max_length=50)
    Incident = models.IntegerField(default=0)
    Date = models.DateTimeField(max_length=150,default="")
    Location = models.PointField()

When I try to create a migration, I get the error:

ValueError: Cannot use object with type float for a geometry lookup parameter.

When I try to create a crime object in the Python shell and save it I get the error:

django.core.exceptions.ValidationError

I've been troubleshooting this for quite a while now. I put a debugger in the actual gis library code and found that right before the ValueError was thrown, it was trying to process the number 35.0 as a geometry object.

I'm wondering if this error either has something to do with needing to set defaults in Django or if something is possibly wrong with my database connection?

JJD
  • 50,076
  • 60
  • 203
  • 339
snorkelzebra
  • 663
  • 1
  • 7
  • 17

1 Answers1

11

It turns out than passing in a default as an array of lat + long , which normally works to create a Point was not a valid option for gis to process when interpreting the schema. Here is a good default for gis point fields should anyone need one.

Figured out this solution by reading through this very old conversation: http://south.aeracode.org/ticket/599

Location = models.PointField(default='POINT(0.0 0.0)')
snorkelzebra
  • 663
  • 1
  • 7
  • 17
  • Your solution [does not work for me](http://stackoverflow.com/questions/32507033/how-to-access-a-geometry-point-field-in-postgis-database-from-django) - maybe you have an advice. – JJD Sep 10 '15 at 20:14
  • 1
    @JJD It was an error in the settings.py file. This answer still seems to work great! – raratiru Sep 03 '16 at 18:09