DRF"字段是必需的" POST请求错误?

时间:2018-02-01 21:53:09

标签: django post django-rest-framework

curl --user testuser:passwordz -d '{"name":"testplatform"}' -X POST http://localhost:8080/api/v1/platforms

错误

{"name":["This field is required."]}%

views.py

class platform_list(APIView):

    def get(self, request, format=None):
        query = request.GET.get('name')
        if query:
            platforms = Platform.objects.filter(name=query)
        else:
            platforms = Platform.objects.all()
        serializer = PlatformSerializer(platforms, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = PlatformSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我知道auth有效,因为如果我键入错误的用户或将错误更改传递给"无效的凭据" 。为什么我会收到字段所需的错误?

1 个答案:

答案 0 :(得分:5)

我忘了在POST请求中添加“-H”Content-Type:application / json“。