我是DRF的新手,可浏览的API有问题。 我想使用常规的APIView在“原始数据”选项卡的“内容”中填充一个空的JSON,以便用户查看请求的外观。
当我使用CreateModelMixin时它可以工作,但是我想在创建后返回与整个模型不同的东西,或者甚至拥有只做一些计算而没有任何模型底层的路线。
int graphCreate(Graph **, int);
int graphAddPath(Graph *, int, int, unsigned int);
int graphRemovePath(Graph *, int, int);
int graphDestroy(Graph **);
对于上面的示例,我希望可浏览的API在“原始数据”标签的“内容”字段中显示空的JSON:
# serializers.py----------------------
class AnalysisSerializer(serializers.ModelSerializer):
requestor = serializers.ReadOnlyField(source='requestor.username')
results = serializers.ReadOnlyField()
class Meta:
model = Analysis
fields = ('requestor','text','results')
# views.py----------------------
class AnalysisRequest(APIView):
"""
Request text analysis.
"""
serializer_class = AnalysisSerializer
def post(self, request):
serializer = AnalysisSerializer(data=request.data)
if serializer.is_valid():
text = serializer.validated_data['text']
results = analyze.analyze_text(text)
serializer.save(requestor=request.user, results=results)
return Response(results, status=status.HTTP_200_OK)
else:
return Response({'error': 'Invalid JSON received.'}, status=status.HTTP_400_BAD_REQUEST)
有没有简单的方法可以做到这一点?