如何从Django访问PostGIS数据库中的几何(点)字段? [英] How to access a geometry (point) field in PostGIS database from Django?
问题描述
在我的项目中,我使用PostgreSQL / PostGIS作为数据库,Django使用 django.contrib.gis
。 现有表 pois
包含地理空间点数据。以下是SQL创建语句的摘录:
CREATE TABLE pois
b $ b ogc_fid serial NOT NULL,
the_geom几何(Point,900914),
名称字符变化(254),
- ...
我使用以下命令生成了Django模型:
$ python manage.py inspectdb
生成的模型看起来像这个:
from django.contrib.gis.db import models
类POI(models.Model):
ogc_fid = models.AutoField(primary_key = True)
the_geom = models.TextField(blank = True,null = True)#这个字段类型是一个猜测b $ b name = models.CharField(max_length = 254,blank = True,null = True)
class Meta:
managed = False
db_table ='pois'
我添加以下字段以覆盖具有GeoDjango特定实例的默认管理:
对象= models.GeoManager()
现在,我要替换 the_geom = models .TextField()
与正确的数据类型应该是 models.PointField()
或 models.GeometryField()
。我试过两个然后我在Python shell中测试模型:
$ python manage.py shell
Python 3.4.3(默认,2015年7月28日,18:20:59)
在[1]:从berlin导入模型
在[2]:models.POIs.objects.first()
此操作失败,并输出以下 stacktrace :
/home/user/.virtualenvs/myproject/lib/python3.4/site-packages/django/contrib/ \
gis / db / models / fields.py in select_format(self,compiler,sql,params)
57 else:
58 sel_fmt ='%s'
---> 59 if connection.ops.select:
60#这允许在SELECT,
61#中的字段上执行操作,覆盖它们的值 - 由Oracle和MySQL
AttributeError:'DatabaseOperations'对象没有属性'select'
有没有错误当我离开模型与 models.TextField
。然后输出字符串值。
settings.py 中的数据库配置不正确: / p>
DATABASES = {
/ pre>
'default'
'ENGINE':'django.db.backends.postgresql_psycopg2',
'NAME':'geodjango',
'USER':'geo',
'PASSWORD' 'secret',
'HOST':'localhost',
'PORT':'5432',
}
}
正确:
DATABASES = {
'default':{
'ENGINE':'django.contrib.gis.db.backends.postgis',#这里
'NAME':'geodjango',
'USER':'geo',
'PASSWORD':'secret',
'HOST':'localhost',
'PORT':'5432',
}
我已经在文档不知何故。感谢来自
#django
irc频道的 apollo13 ,以便向正确的方向发问。In my project I am using PostgreSQL/PostGIS as the database and Django with
django.contrib.gis
configured. The existing tablepois
contains geospatial point data. Here is a excerpt from the SQL create statement:CREATE TABLE pois ( ogc_fid serial NOT NULL, the_geom geometry(Point,900914), name character varying(254), -- ...
I generated the Django model with the following command:
$ python manage.py inspectdb
The generated model looks like this:
from django.contrib.gis.db import models class POIs(models.Model): ogc_fid = models.AutoField(primary_key=True) the_geom = models.TextField(blank=True, null=True) # This field type is a guess. name = models.CharField(max_length=254, blank=True, null=True) class Meta: managed = False db_table = 'pois'
I add the following field to overwrite the default manage with a GeoDjango specific instance:
objects = models.GeoManager()
Now, I want to replace
the_geom = models.TextField()
with the correct data type which should be eithermodels.PointField()
ormodels.GeometryField()
. I tried both. Then I test the model at the Python shell:$ python manage.py shell Python 3.4.3 (default, Jul 28 2015, 18:20:59) In [1]: from berlin import models In [2]: models.POIs.objects.first()
This fails and the following stacktrace is output:
/home/user/.virtualenvs/myproject/lib/python3.4/site-packages/django/contrib/ \ gis/db/models/fields.py in select_format(self, compiler, sql, params) 57 else: 58 sel_fmt = '%s' ---> 59 if connection.ops.select: 60 # This allows operations to be done on fields in the SELECT, 61 # overriding their values -- used by the Oracle and MySQL AttributeError: 'DatabaseOperations' object has no attribute 'select'
There is no error when I leave the model with
models.TextField
. Then the string value is output.解决方案The database configuration in settings.py was incorrect:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'geodjango', 'USER': 'geo', 'PASSWORD': 'secret', 'HOST': 'localhost', 'PORT': '5432', } }
Correct:
DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', # Here 'NAME': 'geodjango', 'USER': 'geo', 'PASSWORD': 'secret', 'HOST': 'localhost', 'PORT': '5432', } }
I have overseen this in the documentation somehow. Thanks to apollo13 from the
#django
irc channel for the advice into the right direction.这篇关于如何从Django访问PostGIS数据库中的几何(点)字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!