TL; DR
How to create both serializers.Patient
and serializers.Temperature
in such way that:
models.Patient
has one-to-many relationship withmodels.Temperatures
serializers.Patient
is a subclass ofserializers.ModelSerializer
serializers.Patient
(de)serialize temperatures as a list of floats
Details
Given a quick-dirty patient medical records RESTful API implemented with Django framework.
Patient is defined at models.Patient
as:
class Patient(models.Model):
created_at = models.DateField()
name = models.CharField(max_length=200)
updated_at = models.DateField()
and the models.Temperature
:
class Temperature(models.Model):
created_at = models.DateField()
patient = models.ForeignKey(
Patient,
db_column='patient',
related_name='temperatures',
on_delete=models.CASCADE,
)
updated_at = models.DateField()
value = models.DateField()
CRUD operations at /patients
(de)serialized models.Temperature
as float
lists, thus a POST
should only require:
{
"name": "John Connor",
"temperatures": [36.7, 40, 35.9]
}
while a GET
operation
{
"created_at": "1985-03-25",
"name": "John Connor",
"temperatures": [36.7, 40, 35.9],
"updated_at": "2021-08-29"
}
However operations at /patients/<id>/temperatures/
endpoint should return all properties:
[
{
"created_at": "1985-03-25",
"value": 36.7,
"updated_at": "2021-08-29"
},
{
"created_at": "1985-03-25",
"value": 40.0,
"updated_at": "2021-08-29"
},
{
"created_at": "1985-03-25",
"value": 35.9,
"updated_at": "2021-08-29"
}
]
Can this feature be implemented subclassing standards DRF serializers or does it require a customized serializers.Serializer
subclass?
from Recent Questions - Stack Overflow https://ift.tt/3okD8O8
https://ift.tt/eA8V8J
No comments:
Post a Comment