gpt4 book ai didi

python - 序列化程序调用显示 TypeError : Object of type 'ListSerializer' is not JSON serializable?

转载 作者:行者123 更新时间:2023-12-03 13:44:01 24 4
gpt4 key购买 nike

我试图在 django GET 请求中调用序列化程序但它显示了一个
类型错误:“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

最佳答案

问题是您将序列化程序本身而不是序列化数据发送到响应。您应该将其更改为:

self.data = objects.data

但这就是说,大部分代码是完全没有必要的,您已经绕过了 Django 和 DRF 为您所做的大部分 .he 事情。删除您的 mixin 并使用 ListAPIView。

关于python - 序列化程序调用显示 TypeError : Object of type 'ListSerializer' is not JSON serializable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54343170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com