类型为SerializerMetaclass的对象不是JSON可序列化的DRF

时间:2019-01-31 15:53:51

标签: django django-rest-framework python-requests

我想使用requestsdrf对数据进行编码并创建post方法,因此示例是

class ProjectViewSet(viewsets.ModelViewSet):
    logger = logging.getLogger('django')

    """
    API 
    """
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    filter_backends = (filters.OrderingFilter,
                       filters.SearchFilter, DjangoFilterBackend)
    filterset_class = ProjectFilter
    ordering_fields = ('name', 'owner', 'project')
    ordering = ('name')
    search_fields = ['name']

    # create new execution for this project
    def create(self, request):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            encode_data = json.dumps(serializer)
            response = requests.post(
                url='{}/process'.format(EXAMPLE_URL),
                json=encode_data,
                headers=headers
            )
            logging.debug(f"{self.response}")
            return Response({'response': response})

此错误

Object of type SerializerMetaclass is not JSON serializable

现在我有点困惑,我想我需要对数据进行编码,因为JSON编码是在Response对象中完成的,但是我无法理解该错误,所以有人可以在这里解释出什么问题了,谢谢

0 个答案:

没有答案