Search code examples
djangodjango-rest-frameworkgeojsongeodjangodjango-rest-framework-gis

Error when serializing foreign key object with django-rest-framework-gis to geojson


I am trying to serialize into geojson a model with a foreign key that has a geometry field. I am using the django-rest-framework-gis. I am using django 2.2

I'm stuck on this and none of the other related answers on Stack Overflow work for me. e.g. I have tried versions of this:

The problem is I get this error: Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view, but received a <class 'statements.models.Response'>

Models:

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

Views

class ResponseHeatmapAPIView(APIView): #ReadOnly

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

Serializers

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',)

Traceback

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

Solution

  • Serializer is waiting for data from you. I got the same error when I tried your view with a simple modeling. I think there is something wrong with the definition. can you try the presentation:

    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)
    

    EDITED:

    Please excuse me because of my sleeplessness.

    The BIG problem here is the name of the table: Response. You have to change the name; somehow it conflicts with the 'Response' that the RestAPI uses.

    • Change Response(model.Model)'s name to different name.
    • python manage.py makemigrations
    • python manage.py migrate

    and the view is ready to run (I changed the model name to '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)