Home > Software design >  Django - Object of type FloatField is not JSON serializable
Django - Object of type FloatField is not JSON serializable

Time:09-04

I have a service in django, and I am trying to get the list of all vital signs, but when I run it, it throws me the following error.

enter image description here

This is the model "SignoVital".

from django.db import models

from .paciente import Paciente


class SignoVital(models.Model):
    oximetria = models.FloatField()
    frecuenciaRespiratoria = models.FloatField()
    frecuenciaCardiaca = models.FloatField()
    temperatura = models.FloatField()
    presionArterial = models.FloatField()
    glicemia = models.FloatField(),
    paciente = models.ForeignKey(
        Paciente,
        on_delete=models.CASCADE,
        unique=False,
        blank=True,
        null=True
    )

This is serializer "SignoVitalSerializer".

from rest_framework import serializers
from hospiApp.models.signoVital import SignoVital

class SignoVitalSerializer(serializers.ModelSerializer):
    class Meta:
        model = SignoVital
        fields = ['oximetria', 'frecuenciaRespiratoria', 'frecuenciaCardiaca', 'temperatura', 'presionArterial', 'glicemia', 'paciente']

This is View "SignoVitalTodosView".

from rest_framework import generics,status, views
from rest_framework.response import Response
from rest_framework.views import APIView

from hospiApp.models.signoVital import SignoVital
from hospiApp.serializers.signoVitalSerializer import SignoVitalSerializer

class SignoVitalTodosView(generics.ListCreateAPIView):
    queryset = SignoVital.objects.all()
    serializer_class = SignoVitalSerializer
    def get(self, request, *args, **kwargs): 
        return super().get(request, *args, **kwargs)

This is urls.py

from django.contrib import admin
from django.urls import path
from hospiApp import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('signoVitalTodos/', views.SignoVitalTodosView.as_view()),    
]

CodePudding user response:

Remove the comma after the glicemia = models.FloatField(), line in the SignoVital model and then create the migrations again and apply them using the following commands.

python manage.py makemigrations
python manage.py migrate

This will fix the issue.

  • Related