如何分配Django PointField模型属性?

时间:2014-07-23 21:40:57

标签: python django geodjango

您好我有一个Django模型如下:

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

现在我创建了这个模型的一个实例:

A = Address()

如何将A的geoCoord字段的坐标设置为(5.3,6.2)?我找不到以这种方式分配点字段的任何示例。这是一个非常简单的问题。实际上,我想分配给A.geoCord的坐标来自pygeocoder。它是一个2项元组的花车。

关于Django PointFields的文档基本上是non-existant

2 个答案:

答案 0 :(得分:14)

在较新版本的Django中,您可以:

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

https://docs.djangoproject.com/en/1.11/ref/contrib/gis/geos/

中提供了大量示例

答案 1 :(得分:8)

您可以使用:

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

其中latlon分别表示latitudelongitudesrid是可选的,表示Spatial Reference System Identifier

您可以在此处详细了解如何绘制不同的几何图形:https://docs.djangoproject.com/en/dev/ref/contrib/gis/geos/#what-is-geos

相关问题