我想使用requests
和drf
对数据进行编码并创建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对象中完成的,但是我无法理解该错误,所以有人可以在这里解释出什么问题了,谢谢