DRF Methodserializer:<XXX> 类型的对象不是 JSON 可序列化的

DRF Methodserializer : Object of type <XXX>is not JSON serializable

我正在尝试将现有项目转换为 DRF。但是我面临以下错误:

: Object of type TransitionApproval is not JSON serializable

TransitionApproval 对象来自一个名为 django-river 的包。这是我的代码:

class ProjectDetailSerializer(serializers.ModelSerializer):
requirements = CustomerRequirementSerializer(many=True)
transitionApproval = serializers.SerializerMethodField('get_transition_approval')

class Meta:
    model = Project
    fields = '__all__'
    depth = 2

def get_transition_approval(self,project):
    transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
    print(transitions)
    return transitions

我的控制台打印:

<CTEQuerySet [<TransitionApproval: TransitionApproval object (1)>, <TransitionApproval: TransitionApproval object (2)>, <TransitionApproval: TransitionApproval object (3)>]>

有什么好的方法可以解决吗?我试过这样做:

class ProjectDetailSerializer(serializers.ModelSerializer):
requirements = CustomerRequirementSerializer(many=True)
transitionApproval = TransitionSerializer(many=True)

class Meta:
    model = Project
    fields = '__all__'
    depth = 2

不过好像'transitionApproval'不是Project的属性。我不确定如何解决这个问题。

这是我的详情页浏览量

class SalesProjectDetailView(RetrieveAPIView):
queryset = SalesProject.objects.all()
serializer_class = SalesProjectDetailSerializer

编辑:

我听取了建议,在返回之前通过序列化程序传递查询集。

class TransitionApprovalSerializer(serializers.ModelSerializer):
    class Meta:
        model = TransitionApproval
        fields = '__all__'    


class ProjectDetailSerializer(serializers.ModelSerializer):
    transitionApproval = serializers.SerializerMethodField('get_transition_approval')

    class Meta:
        model = Project
        fields = '__all__'
        depth = 2

    def get_transition_approval(self,project):
        transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
        return TransitionApprovalSerializer(transitions).data

然而 returns 我的错误是:

Traceback (most recent call last):

文件“C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-

packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\generics.py", line 208, in get
    return self.retrieve(request, *args, **kwargs)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\mixins.py", line 56, in retrieve
    return Response(serializer.data)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 562, in data
    ret = super().data
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 260, in data
    self._data = self.to_representation(self.instance)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 529, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\fields.py", line 1905, in to_representation
    return method(value)
  File "C:\Users\dream\Desktop\crmReact\backend\backend\sales\api\serializers.py", line 205, in get_transition_approval
    return TransitionApprovalSerializer(transitions).data
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 562, in data
    ret = super().data
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 260, in data
    self._data = self.to_representation(self.instance)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\serializers.py", line 516, in to_representation
    attribute = field.get_attribute(instance)
  File "C:\Users\dream\Desktop\crmReact\backend\react_env\lib\site-packages\rest_framework\fields.py", line 487, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `object_id` on serializer `TransitionApprovalSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `CTEQuerySet` instance.
Original exception text was: 'CTEQuerySet' object has no attribute 'object_id'.

object_id 是 TransitionApproval 对象的一个​​字段,但我认为序列化程序将查询集作为对象本身

您可能会遇到此错误,因为您的 get_transition_approval 方法 return 查询集 TransitionApproval。查询集无法直接序列化为 json。您必须 return 来自此方法的序列化数据。您可以创建另一个序列化程序并使用此序列化程序序列化您的数据:

class TransitionApprovalSerializer(serializers.ModelSerializer):#your new serializer
    class Meta:
        model = TransitionApproval
        fields = [...]


class ProjectDetailSerializer(serializers.ModelSerializer):
    ....
    def get_transition_approval(self,project):
        transitions = TransitionApproval.objects.filter(object_id=project.pk).filter(workflow__field_name='project_status')
        # you can serialize your data here
        return TransitionApprovalSerializer(transitions).data