How to assign to a Django PointField model attribute?

房东的猫 提交于 2019-11-29 00:47:25

问题


Hi I have a Django Model as follows:

class Address(models.Model):
    geoCoords = models.PointField(null=True, blank=True,)

Now I create an instance of this model:

A = Address()

How can I set the coordinates of A's geoCoord field to (5.3, 6.2)? I can't find any example of where a point field is assigned to in this way. It's such a stupidly simple question. Actually, the coordinates I would like to assign to A.geoCord are from pygeocoder. It is a 2-item tuple of floats.

The documentation on Django PointFields is basically non-existant.


回答1:


In newer versions of Django you can:

from django.contrib.gis.geos import Point
pnt = Point(1, 1)

Plenty of examples available in https://docs.djangoproject.com/en/1.11/ref/contrib/gis/geos/




回答2:


You can use:

from django.contrib.gis.geos import GEOSGeometry
A.geoCoords = GEOSGeometry('POINT(LON LAT)', srid=4326) # 

where lat and lon mean latitude and longitude, respectively and srid is optional, indicating the Spatial Reference System Identifier.

You can see more on how to draw different geometries here: https://docs.djangoproject.com/en/dev/ref/contrib/gis/geos/#what-is-geos



来源:https://stackoverflow.com/questions/24921545/how-to-assign-to-a-django-pointfield-model-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!