大家好,我有错误对象类型' MyUser'是不是JSON可序列化,我在很多帖子上有相同的问题,但似乎没有任何东西使我的代码工作
有人可以帮帮我吗?在我的观点中:
class TeamChartData(APIView):
queryset = MyUser.objects.all()
serializer_class = MyUserSerializer
permission_classes = []
http_method_names = ['get',]
def get_serializer_class(self):
return self.serializer_class
def get(self, request, format=None, *args, **kwargs):
chunk_team = get_chunk_team(self)
data = {
"chunk_team":chunk_team
}
return Response(data)
def get_chunk_team(self, format=None, *args, **kwargs):
current_team_member = Project.objects.get(id = self.kwargs['pk']).team_id.members.all()
print(current_team_member)
return current_team_member
serializer.py:
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from registration.models import MyUser
from website.models import Team,Project
from survey.models import Response
class MyUserSerializer(ModelSerializer):
team = serializers.SerializerMethodField()
class Meta:
model = MyUser
fields = (
'email',
'first_name',
'last_name',
'team',
)
def get_team(self, obj):
#print(obj) # for you to understand what's happening
teams = Team.objects.filter(members=obj)
serialized_teams = TeamSerializer(teams,many=True)
return serialized_teams.data
class TeamSerializer(ModelSerializer):
class Meta:
model = Team
fields = (
'team_name',
'team_hr_admin',
'members',
)
我添加了我的日志,以便您查看正在发生的事情 日志:
<QuerySet [<MyUser: rise1@gmail.com>, <MyUser: rise2@gmail.com>, <MyUser: rise3@gmail.com>]>
Internal Server Error: /website/project/2/api/chart/data2/
Traceback (most recent call last):
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/exception.py", li
ne 41, in inner
response = get_response(request)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 21
7, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 21
5, in _get_response
response = response.render()
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/django/template/response.py", line 107
, in render
self.content = self.rendered_content
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/response.py", line 72,
in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/renderers.py", line 706
, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/renderers.py", line 664
, in get_context
'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/renderers.py", line 411
, in get_content
content = renderer.render(data, accepted_media_type, renderer_context)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/renderers.py", line 105
, in render
separators=separators
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 438, in _iterencode
yield from _iterencode(o, _current_indent_level)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 428, in _iterencode
yield from _iterencode_list(o, _current_indent_level)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 437, in _iterencode
o = _default(o)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/site-packages/rest_framework/utils/encoders.py", lin
e 70, in default
return super(JSONEncoder, self).default(obj)
File "/Users/raphaelbendenoun/anaconda/envs/myDjangoEnv/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'MyUser' is not JSON serializable
[21/Dec/2017 14:49:11] "GET /website/project/2/api/chart/data2/ HTTP/1.1" 500 158505
答案 0 :(得分:0)
对应的观点如下:
class TeamChartData(APIView):
queryset = MyUser.objects.all()
serializer_class = MyUserSerializer
permission_classes = []
http_method_names = ['get',]
def get_serializer_class(self):
return self.serializer_class
def get(self, request, format=None, *args, **kwargs):
chunk_team = get_chunk_team(self)
chunk_team = MyUserSerializer(chunk_team).data #included line
data = {
"chunk_team":chunk_team
}
return Response(data)
def get_chunk_team(self, format=None, *args, **kwargs):
current_team_member = Project.objects.get(id = self.kwargs['pk']).team_id.members.all()
print(current_team_member)
return current_team_member