2017-12-21 28 views
0

안녕하세요, 'MyUser'유형의 객체가 JSON 직렬 가능하지 않습니다. 동일한 문제가있는 많은 게시물에서 Checke하지만 아무 것도 내 코드 작업을하지 못합니다.JSON 직렬화 불가능한 django REST 프레임웍

누군가 나를 도와 드릴까요? 내보기에

:

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 :

을 : 당신이 장면을 로그를 behing에 무슨 일이 일어나고 있는지에 모습을 가지고 내가 순서대로 내 로그를 추가
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: [email protected]>, <MyUser: [email protected]>, <MyUser: [email protected]>]> 
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

내 편집 된 게시물보기 – Ben2pop

답변

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