0

我正在尝试使用 django-rest-gis 重新调整我的后端,以使 POST 请求将数据添加到数据库并 GET 请求列出数据。但是,我收到以下错误:-

Internal Server Error: /api/
Traceback (most recent call last):
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/generics.py", line 241, in get
    return self.list(request, *args, **kwargs)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/mixins.py", line 48, in list
    return Response(serializer.data)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 20, in data
    return super(ListSerializer, self).data
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 28, in to_representation
    ("features", super(GeoFeatureModelListSerializer, self).to_representation(data))
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework_gis/serializers.py", line 100, in to_representation
    fields = list(self.fields.values())
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1031, in get_fields
    field_names, declared_fields, extra_kwargs
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1378, in get_uniqueness_extra_kwargs
    field_names, declared_fields, extra_kwargs
  File "/home/vineet/env-tsl/lib/python3.6/site-packages/rest_framework/serializers.py", line 1450, in _get_model_fields
    if field_name in declared_fields:
TypeError: unhashable type: 'list'
[25/May/2019 04:48:29] "GET /api/ HTTP/1.1" 500 147185

我在stackoverflow上关注了很多答案,但只有当你有一个可变类型作为字典中的键时才能得到它

模型.py

from django.contrib.gis.db import models


# Create your models here.

class test1(models.Model):

    date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    location = models.PointField(blank=False)
    plg = models.PolygonField(srid=4326, geography=True, null=True, blank=True)
    city = models.CharField(max_length=50, blank=False)
    state = models.CharField(max_length=50, blank=False)


    def __str__(self):
        return "%s" % (self.state)

序列化程序.py

from rest_framework_gis.serializers import GeoFeatureModelSerializer
from .models import test1


class test1Serializer(GeoFeatureModelSerializer):
    class Meta:
        model = test1
        geo_field = ['location','plg']
        auto_bbox = True
        id_field = False

        fields = ('city','state')

视图.py

from .models import test1
from .serializers import test1Serializer
from rest_framework.generics import ListCreateAPIView

class test1SerializerCreate(ListCreateAPIView):
    serializer_class = test1Serializer
    queryset = test1.objects.all()
4

2 回答 2

1

GeoFeatureModelSerializer只需要一个字段而不需要列表,检查一个字段,它应该可以工作。对多个字段使用 Rest,您可以在视图中解析。或者您可以创建一个新GeometrySerializerMethodField字段并在其中返回两个几何图形

于 2019-05-25T05:37:32.907 回答
1

geo_field 不能是列表。

它可以是这样的:

class test1Serializer(GeoFeatureModelSerializer):
    m_geo_field = GeometrySerializerMethodField()

    def get_m_geo_field(self, obj):
        return # do whatever you like

    class Meta:
        model = Location
        geo_field = 'm_geo_field'
于 2019-05-25T05:41:25.287 回答