0

I've seen some of the similar questions on this subject, but the structure of other posters' code is different than the tutorial I followed for building the REST api (http://www.django-rest-framework.org/tutorial/quickstart/). Following the tutorial, I get an unnamed JSON response when querying the API. I have serializers.py and views.py as the two files that process the JSON:

serializers.py:

from rest_framework import serializers
from main.models import Request

class RequestSerializer(serializers.ModelSerializer):

    class Meta:
        model = Request
        fields = ('user', 'request', 'time')

views.py

class RequestViewSet(viewsets.ModelViewSet):
    queryset = Request.objects.all().order_by('-time')
    serializer_class = RequestSerializer
    paginate_by = None

Other solutions have been along the lines of adding return Response({"data": serializer.data}), but I'm unsure where I could add this in my code.

1 Answer 1

1
class RequestSerializer(serializers.ModelSerializer):
     results_field = "your selected name"

or you can override the list method

def list(self, request, *args, **kwargs):
    self.object_list = self.filter_queryset(self.get_queryset())
    serializer = self.get_serializer(self.object_list, many=True)
    return Response({'results': serializer.data})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Exprator. The second method worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.