Issue
Iam trying to call a serializer in django GET request But it shows up an TypeError: Object of type 'ListSerializer' is not JSON serializable Please help me if i went wrong somewhere. Thanks in Advance.
class Member(MemberMixin, APIView):
serializer_class = MemberSerializers
def get(self, request, format=None):
objects = MemberSerializers(Members.objects.all(), many=True)
self.meta_data = "GET"
self.module = "Member"
self.data = objects
if objects is None:
self.error = "datas are not found"
return Response(self.response_obj, status=status.HTTP_404_NOT_FOUND)
else:
return Response(self.response_obj, status=status.HTTP_200_OK)
pass
The serializer code is as follows:
from .models import Members
class MemberSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model = Members
fields = ('username', 'fullname', 'phone','address','memberid')
# exclude = ['']
pass
MemberMixins code as follows:
class MemberMixin(object):
meta_data = dict()
data = dict()
error = dict()
def __init__(self):
self.meta_data = dict()
self.data = dict()
self.error = dict()
self.json_schema = None
super().__init__()
@property
def response_obj(self):
resp = dict()
resp['meta'] = self.meta_data
if self.error:
resp['error'] = self.error
else:
resp['data'] = {
'detail': self.data,
}
return resp
pass
Solution
The problem is that you're sending the serializer itself, rather than the serialized data, to the response. You should change it to:
self.data = objects.data
But that said, most of this code is totally unnecessary, You're bypassing most of .he things that Django and DRF do for you already. Remove your mixin and use a ListAPIView.
Answered By - Daniel Roseman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.