1

我正在尝试将具有几何字段的外键的模型序列化到 geojson 中。我正在使用 django-rest-framework-gis。我正在使用 django 2.2

我坚持这一点,Stack Overflow 上的其他相关答案都不适合我。例如,我尝试过以下版本:

问题是我收到此错误:预期 a ResponseHttpResponseHttpStreamingResponse要从视图返回,但收到 a<class 'statements.models.Response'>

楷模:

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point

class Hexgrid_10km2(models.Model):
    lng = models.FloatField()
    lat = models.FloatField()
    polygon = models.MultiPolygonField(srid=4326)
    centroid = models.PointField(default=Point(0,0), srid=4326)

    def __str__(self):
        return f'lng: { self.lng } lat: {self.lat }'


class Response(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    statement = models.ForeignKey(Statement, on_delete=models.CASCADE)
    hexgrid_10km2 = models.ForeignKey(Hexgrid_10km2, on_delete=models.CASCADE, null=True, blank=True)

    @property
        def polygon(self):
            return self.hexgrid_10km2.polygon

意见

class ResponseHeatmapAPIView(APIView): #ReadOnly

    def get(self, request, pk):
        final = Response.objects.all()
        serializer = ResponseHeatmapSerializer(final, many=True)
        return Response(serializer.data)

序列化器

class ResponseHeatmapSerializer(GeoFeatureModelSerializer):
    """ A class to serialize hex polygons as GeoJSON compatible data """

    hexgrid_10km2 = GeometrySerializerMethodField()

    def get_hexgrid_10km2(self, obj):
        return obj.hexgrid_10km2.polygon

    class Meta:
        model = Response
        geo_field = 'hexgrid_10km2'
        id_field = False
        fields = ('id',)

追溯

OrderedDict([('type', 'FeatureCollection'), ('features', [OrderedDict([('id', 2), ('type', 'Feature'), ('geometry', GeoJsonDict([('type', 'MultiPolygon'), ('coordinates', [[[[0.52353663711945, 50.8631481850499], [0.538799411254891, 50.86870974483], [0.554062185390241, 50.8631481850499], [0.554062185390241, 50.852023074998], [0.548895002474644, 50.85013962], [0.540831980000019, 50.85013962], [0.53199000192362, 50.8489417564374], [0.52353663711945, 50.852023074998], [0.52353663711945, 50.8631481850499]]]])])), ('properties', OrderedDict([('response_date', '2020-04-15T21:04:04.599597Z'), ('agree', 1), ('hasLocation', False), ('location', GeoJsonDict([('type', 'Point'), ('coordinates', [0.550660257722784, 50.851541821108924])])), ('latitude', 50.851541821108924), ('longitude', 0.5506602577227836), ('locationAccuracy', 0.0), ('user', 1), ('statement', 1)]))])])])
Internal Server Error: /statements/api/heatmap/1
Traceback (most recent call last):
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\rest_framework\views.py", line 507, in dispatch
    self.response = self.finalize_response(request, response, *args, **kwargs)
  File "C:\Users\anton\OneDrive\Documents\django\karate-project-2.2\venv\lib\site-packages\rest_framework\views.py", line 419, in finalize_response
    assert isinstance(response, HttpResponseBase), (
AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'statements.models.Response'>`
[15/Apr/2020 21:55:04] "GET /statements/api/heatmap/1 HTTP/1.1" 500 89414
4

1 回答 1

1

序列化程序正在等待您的数据。当我使用简单的建模尝试您的视图时,我遇到了同样的错误。我认为定义有问题。你能试试演示吗:

def get(self, request):
    final = Response.objects.all()
    serializer = ResponseHeatmapSerializer(data=final, many=True)
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data, status=status.HTTP_201_CREATED)

编辑:

请原谅我,因为我失眠了。

这里最大的问题是表的名称:Response。您必须更改名称;不知何故,它与 RestAPI 使用的“响应”冲突。

  • 将 Response(model.Model) 的名称更改为不同的名称。
  • python manage.py makemigrations
  • python manage.py 迁移

并且视图已准备好运行(我将模型名称更改为'Strawberry'

class ResponseHeatmapList(APIView):
    def get(self, request):
        final = Strawberry.objects.all()
        serializer = ResponseHeatmapSerializer(final, many=True)
        return Response(serializer.data)


class ResponseHeatmapDetail(APIView):
    def get_object(self, pk):
        try:
            return Strawberry.objects.get(pk=pk)
        except Strawberry.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        final = self.get_object(pk)
        serializer = ResponseHeatmapSerializer(final)
        return Response(serializer.data)
于 2020-04-15T22:29:43.687 回答