@api_view(['GET', 'POST'])
@csrf_exempt
def index(request):
if request.method == 'GET':
queryset = Test.objects.all()
return render(request, 'rest_framework/index.html', {'request':request, 'queryset':queryset})
elif request.method == 'POST':
post_mutable = request.POST.copy()
serializer = TestSerializer(data=request.DATA)
serializer.context = {'request': request}
if serializer.is_valid():
serializer.data['test'] = "testing"
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
如何在保存之前更改数据值?我想更改TextField" test"。