Editing serialized viewsets response to include additional value, and overriding viewset response?

Asked over at SO, we’re trying to include an additional value in our responses that JS can use alongside the original response which is used as a geojson. Please see question for full context.

Someone pointed out responses can be adjusted in views.py, which makes total sense, except we’re using viewsets.py, as adapted from @pauloxnet’s great map tutorial. We thought we should try to override the MarkerViewSet(viewsets.ReadOnlyModelViewSet) response as discussed here, but retrieve() doesn’t seem to do anything. What we hoped might work:

class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
    bbox_filter_field = "location"
    filter_backends = (filters.InBBoxFilter,)
    queryset = Marker.objects.all()
    serializer_class = MarkerSerializer

    def retrieve(self, queryset, request, *args, **kwargs):
        print("retrieve method was used") # not being printed
        return({'new_value': "foo", 'geojson': queryset})

Are we going about this the right way? How can we just include another value that JS can access with something like newResponse.new_value and existing functionality can continue getting the same data via newResponse.geojson or similar?

Thanks for any help!

After a few days of going insane I figured it out with some help from a much better programmer. Please see updated SO thread. Thanks anyway!

I’m happy that you found my web map article series useful :pray:

1 Like