在我的项目中,我使用PostgreSQL/PostGIS作为数据库,并配置了Django和django.contrib.gis
。现有的表pois
包含地理空间点数据。以下是SQL创建语句的一部分:
CREATE TABLE pois
(
ogc_fid serial NOT NULL,
the_geom geometry(Point,900914),
name character varying(254),
-- ...
我使用以下命令生成了 Django 模型:
$ python manage.py inspectdb
生成的模型如下所示:
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'
我添加了以下字段来使用GeoDjango特定的实例覆盖默认管理器:
objects = models.GeoManager()
现在,我想要用正确的数据类型替换the_geom = models.TextField()
,应该是models.PointField()
或models.GeometryField()
。我尝试了两者。然后在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()
这个操作失败了,下面是输出的堆栈跟踪信息:
/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'
当我使用 models.TextField
离开模型时,没有错误发生。然后输出字符串值。