0

I'm new to django rest framework. In my project i need to send api on a request, here i'm not using model data instead i want to send a dictionary as api response which is read from external database like mongodb. how to do this?

viewset code

class LivedataViewSet(viewsets.ModelViewSet):
    queryset = LiveData.objects.all()
    serializer_class = LiveDataSerializer

    def get_queryset(self):
        qs = super().get_queryset()
        user_id = str(self.request.query_params.get('user'))
        if user_id:
            queryset = qs.filter(user=user_id)
            return queryset
        else:
            return qs

and serializer code is

class LiveDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = LiveData
        fields = ('id', 'user', 'status')

this code works but it uses model here i need same function without model.

1 Answer 1

2

You need APIView instead of ModelViewSet, with that you can define your own endpoints, without tying it to a model.

Docs: https://www.django-rest-framework.org/api-guide/views/

Sign up to request clarification or add additional context in comments.

Comments

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.