I set up a Django Rest Framework (DRF) API using django-rest-framework-gis.
It works fine to post data from the API's interface, as long as I keep the geometry field to null.
But as soon as I try including a GeoJSON in the geometry field (as below)...
{
"gmlid": "this is a test from DRF interface",
"name": "",
"envelope": { "type": "Point", "coordinates": [ 5.000000, 23.000000 ] },
"creation_date": null,
"xml_source": ""
}
...I get the following error:
ProgrammingError at /en/api/cityobject/
function st_geomfromewkb(bytea) does not exist LINE 1: ...atest_from_browser', '', '', '', '', ST_Transform(ST_GeomFro... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Request Method: POST Request URL: http://127.0.0.1:8000/en/api/cityobject/ Django Version: 2.2 Exception Type: ProgrammingError Exception Value:
function st_geomfromewkb(bytea) does not exist LINE 1: ...atest_from_browser', '', '', '', '', ST_Transform(ST_GeomFro... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Exception Location: /home/me/dj-workspace/my-venv/lib/python3.6/site-packages/django/db/backends/utils.py in _execute, line 84 Python Executable: /home/me/dj-workspace/my-venv/bin/python Python Version: 3.6.8 Python Path:
['/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']
Server time: Sat, 5 Oct 2019 07:49:35 +0000
What may be causing this error?
My code looks like this:
models.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'
views.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',)
urls.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'))
]
serializers.py
class CityobjectSerializer(GeoFeatureModelSerializer):
class Meta:
model = Cityobject
fields = (
"gmlid",
"name",
"envelope",
"creation_date",
"xml_source"
)
geo_field = 'envelope'
settings.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',
...
]