curl --user testuser:passwordz -d '{"name":"testplatform"}' -X POST http://localhost:8080/api/v1/platforms
错误
{"name":["This field is required."]}%
View .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)
我知道身份验证有效,因为如果我输入错误的用户或将错误更改传递给“无效凭据”。为什么我会收到字段所需的错误?
最佳答案
我忘了在 POST 请求中添加“-H”Content-Type:application/json。
关于django - POST 请求上的 DRF "field is required"错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48572338/