From ea13552f5cffa80b1cf14e85c8f527dfebae5c95 Mon Sep 17 00:00:00 2001 From: Alex Lubbock Date: Tue, 12 Mar 2024 15:09:44 +0000 Subject: [PATCH] fix: encode NaN optical densities as null in JSON --- backend/antigenapi/views.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/antigenapi/views.py b/backend/antigenapi/views.py index ea6cf04..9c88c6e 100644 --- a/backend/antigenapi/views.py +++ b/backend/antigenapi/views.py @@ -1,5 +1,6 @@ import collections.abc import io +import math import os import urllib.error import urllib.parse @@ -23,6 +24,7 @@ FileField, ModelSerializer, PrimaryKeyRelatedField, + SerializerMethodField, StringRelatedField, ValidationError, ) @@ -283,10 +285,18 @@ def perform_create(self, serializer): # noqa: D102 class NestedElisaWellSerializer(ModelSerializer): """A serializer for elisa wells.""" + optical_density = SerializerMethodField() + class Meta: # noqa: D106 model = ElisaWell exclude = ("id", "plate") + def get_optical_density(self, obj): + """Get optical density - convert NaN to None for JSON encoding.""" + if obj.optical_density is not None and not math.isnan(obj.optical_density): + return obj.optical_density + return None + class ElisaPlateSerializer(ModelSerializer): """A serializer for elisa plates.