0

I am trying to program an API with Django Rest Framework and Psycopg2 library. This API works with PostgreSQL database. I need to store 2 float array fields in this db because the API works with 2 sensors which stream data to an Android App, and they are storing in the APP in two arrays and they are sent by JSON to the API REST.
These are my models.py, serializers.py and views_api.py:
Models.py
class DataTest(models.Model):
patient = models.ForeignKey(Patient)
label = models.CharField(max_length=36)
angle_data = ArrayField(models.FloatField())
emg_data = ArrayField(models.FloatField())
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)

def __unicode__(self):
return self.patient

Serializers.py
class DataTestSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
label = serializers.CharField()
angle_data = serializers.ListField(child=serializers.FloatField())
emg_data = serializers.ListField(child=serializers.FloatField())
patient = serializers.PrimaryKeyRelatedField(queryset=Patient.objects.all())

def create(self, validated_data):
return DataTest.objects.create(**validated_data)

def update(self, instance, validated_data):
instance.label = validated_data.get(‘label’, instance.label)
instance.angle_data = validated_data.get(‘angle_data’, instance.angle_data)
instance.emg_data = validated_data.get(’emg_data’, instance.emg_data)
instance.patient = validated_data.get(‘patient’, instance.patient)
instance.save()
return instance

api.py
class DataTestList(APIView):

def get(self, request, format=None):
dataTests = DataTest.objects.all()
serializer = DataTestSerializer(dataTests, many=True)
return Response(serializer.data)

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

Finally, I have tested the API and I have sent an JSON example:
{
“label”:”Test”,
“angle_data”:[1.434, 2.243, 3.234],
“emg_data”:[2.3, 2.1],
“patient”:1
}
and I get the following error:
ProgrammingError: column “angle_data” is of type double precision but expression is of type numeric[] LINE 1: …, “created_at”, “modified_at”) VALUES (1, ‘Test’, ARRAY[1.0,… ^
HINT: You will need to rewrite or cast the expression.
I have debugged the API and i have seen that the serializer receives a float list. I suppose that the problem is that the column in PostgreSQL is a double precision and the float list is not codified with double precision.
Could anybody help me?
Greetings and thank you!!

Kuldeep Baberwal Changed status to publish February 17, 2025