Hi I'm trying to make calculation rest api using django.
My objective is to save preprocessed_data(result) into the db table field
The process is read raw data path(txt file) from db and calculate using pandas & numpy after that make result txt file save to db preprocessed_data field.
this is my models.py
class PreprocessData(models.Model):
raw = models.FileField(
upload_to=_user_directory_path,
storage=OverwriteStorage(),
preprocessed_data = models.CharField(
max_length=200,
null=True,
blank=True,
)
)
and views.py
class PreprocessCalculate(APIView):
def _get_object(self, pk):
try:
data = PreprocessData.objects.get(id=pk)
return data
except PreprocessData.DoesNotExist:
raise NotFound
#get or put or post which is the best design for api?
# PUT API
def put(self, request, pk):
data = self._get_object(pk)
serializer = PreprocessCalculateSerializer(data, request.data, partial=True)
if serializer.is_valid():
updated_serializer = serializer.save()
return Response(PreprocessDataSerializer(updated_serializer).data)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
and serializers.py
class PreprocessResultField(serializers.CharField):
def to_representation(self, value) -> str:
ret = {"result": value.test_func()}
return ret
def to_internal_value(self, data):
return data["result"]
class PreprocessCalculateSerializer(serializers.ModelSerializer):
preprocessed_data = PreprocessResultField()
class Meta:
model = PreprocessData
fields = ("uuid", "preprocessed_data")
my question is
when I use the above code. In db "preprocessed_field" is still null... what is the problem in custom field serializer?
I choose "PUT" method to calculate raw file, but I think if I use "PUT" it has a problem to change my "uuid" by mistake. I think it is not good .. then should I use GET or POST? to make calculation restAPI? If "PUT" is right how to keep idempotent my db?
Pleas help me...