[Django]-Django – add a POINT in a PostgreSQL database using GeoDjango from decimal coordinates

6👍

Here’s what I think you’re asking for:

GeoDjango uses an object relational mapper. In your models.py, you’ll have to define a model that includes a point, e.g:

class my_points(models.Model):
  name = models.CharField(max_length = 100)
  coords = models.PointField()

Then, in one of your views, you’ll need to instantiate a my_points object:

a = my_point(name="example", coords=x,y)

I imagine this isn’t syntactically perfect, but the GeoDjango model api: http://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/ and the regular geodjango models guide: http://docs.djangoproject.com/en/dev/topics/db/models/ will probably get you where you need to go. Hope that helps.

Edit:
Okay, I didn’t understand your post. “add a POINT” indicated to me that you wanted to add one. Your SQL statement is for updating a value, though.

To update values in a database, you’ll still need a model. I’m going to proceed on the assumption that you have a model set up called “my_points”.

from django.contrib.gis.geos import GEOSGeometry
# queries the database for the object with an id=101
a = my_points.objects.get(id=101)
# defines your point
pnt = GEOSGeometry('POINT(5 23)')
# updates the object in memory
a.coords = pnt
# saves the changes to the database
a.save()

Leave a comment