[Answered ]-Serializing model queryset showing empty [OrderedDict()]

0👍

I was using

class BlogSerializer(serializers.Serializer):
    .......

so it was showing empty results (no idea why, I think its deprecated)

After replaceing it with

class BlogSerializer(serializers.HyperlinkedModelSerializer):

It worked

1👍

There is a concept error here. The get_queryset function is not supposed to return serialized data. It must return a QuerySet of model objects.

To achieve what you want you can just do:

class BlogSerializerApiView(viewsets.ModelViewSet):
    serializer_class = BlogSerializer

    def get_queryset(self, *args, **kwargs):
        return Blog.objects.all()

The Django Rest Framework will take care of serializing data.

In fact, you can even do it way more simple. Defining the view’s queryset field like this:

class BlogSerializerApiView(viewsets.ModelViewSet):
    queryset = Blog.objects.all()
    serializer_class = BlogSerializer

Additional:
You said you will relate to current user later. You could achieve that in fact in the get_queryset method filtering aginst the user

class BlogSerializerApiView(viewsets.ModelViewSet):
    serializer_class = BlogSerializer

    def get_queryset(self, *args, **kwargs):
        return Blog.objects.filter(user_id=USER_ID)

Hope this helps!

Leave a comment