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?
GeoDjango is a very powerful framework for storing and working with geographic data using the Django ORM. It provides an easy-to-use API to find the distances between two points on a map, areas of polygons, the points within a polygon, and so on.
The SRID is an integer specifier that corresponds to the projection system that will be used to interpret the data in the spatial database.
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)')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With