我正在尝试在Django GET请求中调用序列化程序,但它显示了一个 TypeError:“ ListSerializer”类型的对象不可JSON序列化 如果我在某个地方出错,请帮助我。预先感谢。
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
序列化器代码如下:
from .models import Members
class MemberSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model = Members
fields = ('username', 'fullname', 'phone','address','memberid')
# exclude = ['']
pass
MemberMixins代码如下:
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
答案 0 :(得分:2)
问题在于您是将序列化器本身而不是序列化的数据发送给响应。您应该将其更改为:
self.data = objects.data
但是,这大部分代码是完全不必要的,您已经绕过了Django和DRF已经为您完成的大部分工作。删除您的mixin并使用ListAPIView。