14

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!!

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
  • 1
    Did you inspect the table via `psql` and verify the underlying column type is correct (e.g., via the `\d [tablename]` command)? The error indicates that the column itself is not an array. – bimsapi Jun 17 '16 at 18:15
  • from your code `angle_data = serializers.ListField(child=serializers.FloatField())` it is not array - is it?.. try passing `1.434` to `angle_data` not `[1.434, 2.243, 3.234]` – Vao Tsun Jun 17 '16 at 21:54
  • Hi!! Thanks for the response. I have inspected the columns and they are `double precisions []`. When I try to store an Array, for example, `[1.434, 2.243, 3.234]` the API responses with the same error. However, I have tried to insert an unique value `1.434 (without brackets)` and it is stored. I also tried to change the `serializer` by a `modelSerializer` because i think that `ListField` works with dictionaries, not lists. However i get the same error :( – Nacho Díaz Reyes Jun 19 '16 at 16:15
  • 2
    Ok, I have gotten to solve this error. The problem was in the form that I wrote the Array. I always write the Array in a standard formulary HTML and I always get the same error. However, if I write the same data in the raw JSON form, I have gotten to store the data perfectly. I don't understand where the problem is in the first way. – Nacho Díaz Reyes Jun 19 '16 at 18:34

1 Answers1

0

I found this post helpful while dealing with JSON data in django models

https://www.laurencegellert.com/2018/09/django-tricks-for-processing-and-storing-json/

Also I use https://github.com/django-json-api/django-rest-framework-json-api for a better formatted Hypermedia RESTful API with DRF. Also https://docs.djangoproject.com/en/dev/releases/4.2/#jsonfield-for-all-supported-database-backends

Also you can try ListField with Child argument.

this is more of a high-level suggestion.

auvipy
  • 769
  • 10
  • 22