0

我使用django-rest-framework-gis设置了 Django Rest Framework (DRF) API 。

只要我将几何字段保留为null. 但是,一旦我尝试在该geometry字段中包含 GeoJSON(如下所示)...

{
    "gmlid": "this is a test from DRF interface",
    "name": "",
    "envelope": { "type": "Point", "coordinates": [ 5.000000, 23.000000 ] },
    "creation_date": null,
    "xml_source": ""
}

...我收到以下错误:

/en/api/cityobject/处的编程错误

函数 st_geomfromewkb(bytea) 不存在 LINE 1: ...atest_from_browser', '', '', '', '', ST_Transform(ST_GeomFro... ^ 提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

请求方法:POST 请求 URL: http: //127.0.0.1 :8000/en/api/cityobject/ Django 版本:2.2 异常类型:ProgrammingError 异常值:

函数 st_geomfromewkb(bytea) 不存在 LINE 1: ...atest_from_browser', '', '', '', '', ST_Transform(ST_GeomFro... ^ 提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

异常位置:/home/me/dj-workspace/my-venv/lib/python3.6/site-packages/django/db/backends/utils.py in _execute,第 84 行 Python 可执行文件:/home/me/dj- workspace/my-venv/bin/python Python 版本:3.6.8 Python 路径:

['/home/me/dj-workspace/myapp', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload' , '/home/me/dj-workspace/my-venv/lib/python3.6/site-packages']

服务器时间:2019 年 10 月 5 日星期六 07:49:35 +0000

什么可能导致此错误?


我的代码如下所示:

模型.py

class Cityobject(models.Model):
    gmlid = models.CharField(max_length=256, blank=True, null=True)
    name = models.CharField(max_length=1000, blank=True, null=True)
    envelope = models.GeometryField(blank=True, null=True, srid=2056)  
    creation_date = models.DateTimeField(blank=True, null=True)
    xml_source = models.TextField(blank=True, null=True)

    def __str__(self):
        return "Name - {0} -- GML ID - {1}".format(self.name, self.gmlid) 

    class Meta:
        managed = False
        db_table = 'cityobject'        

视图.py

class CityobjectViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows Cityobject information to be viewed or edited.
    """    
    lookup_field = 'id' 
    queryset = Cityobject.objects.all()  
    serializer_class = CityobjectSerializer
    filter_fields = ('id','gmlid','name',)   

网址.py

router = routers.DefaultRouter()
router.register(r'cityobject', views.CityobjectViewSet, 'cityobject')
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

序列化程序.py

class CityobjectSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = Cityobject
        fields = (
                    "gmlid",
                    "name",
                    "envelope",
                    "creation_date",
                    "xml_source"
            )    
        geo_field = 'envelope'      

设置.py

DATABASES = {

    'default': 
            {
            },       
    'citydb': 
            {
            'ENGINE': 'django.contrib.gis.db.backends.postgis', 
            'OPTIONS': {
                            'options': '-c search_path=citydb'
                        },
            'NAME': 'db_dev',
            'USER': secrets.USER,
            'PASSWORD': secrets.PASSWORD,
            'HOST': 'my.host.com',
            'PORT': '5432',
            },

}

INSTALLED_APPS = [
    'modeltranslation', #must be before django.contrib.admin
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'main_app',
    'django_extensions',
    'rest_framework',
    'rest_framework_gis',
    'django_filters',
    'leaflet',
     ...
]
4

0 回答 0