I am trying to return custom json with get_queryset
but always get 404 error
in response.
class TestViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Test.objects.all()
serializer_class = TestSerializer
def get_queryset(self):
if self.request.method == "GET":
content = {'user_count': '2'}
return HttpResponse(json.dumps(content), content_type='application/json')
If I delete everything starting from def
I'll got correct response with standard json data. What I am doing wrong?
If you don't need a ModelViewSet and just want custom JSON on a GET request
You can also use an
APIView
, which doesn't require a modeland
With a ModelViewSet
You've put the custom JSON into get_queryset, that's wrong. If you want to use a
ModelViewSet
, this by itself should be enough:This
ModelViewSet
comes with default implementations for.list()
,.retrieve()
,.create()
,.update()
, and.destroy()
. Which are available for you to override (customize) as neededReturning custom JSON from
.retrieve()
and/or.list()
inModelViewSet
E.g. to override
.retrieve()
to return custom view when retrieving a single object. We can have a look at the default implementation which looks like this:So as an example to return custom JSON: