I am working on writing serializers for an existing API for the purpose of auto-generating OpenAPI documentation for the project. One of the issues I'm running into is defining serializers to validate the returned data from some of the views.
Here is the structure of the data:
{
"airmass_data": {
"site-A": {
"times": ["2021-09-09T09:54","2021-09-09T10:04"],
"airmasses": [2.900041850251779, 2.687634707193725]
},
"site-B": {
"times": ["2021-09-09T09:54","2021-09-09T10:04"],
"airmasses": [2.900041850251779, 2.687634707193725]
},
...
},
"airmass_limit": 3.19
}
There can be an arbitrary number of "site-X" keys, and they are dynamically generated - which is definitely part of my issue.
Here is how I've set up my serializers, which I think mostly matches my dictionary structure:
class SiteAirmassDatumSerializer(serializers.Serializer):
times = serializers.ListField(child=serializers.CharField())
airmasses = serializers.ListField(child=serializers.FloatField())
class SiteAirmassSerializer(serializers.Serializer):
site = SiteAirmassDatumSerializer(source='*')
class AirmassSerializer(serializers.Serializer):
airmass_data = SiteAirmassSerializer(source='*')
airmass_limit = serializers.FloatField()
However, when passing my dictionary into the the serializer and attempting to validate it, the serializer.errors attribute has:
{
"airmass_data": {
"site": [
"This field is required."
]
}
}
Is there a good way to write a set of serializers such that it deals with dynamically generated keys? I am mostly trying to write this to validate the general structure of the returned dictionary - not necessarily the keys within it. The reason I am interested in using Serializers is to utilize DRF's OpenAPI generation features.
EDIT:
Have also tried using a DictField in the serializers, like so:
class SiteAirmassDatumSerializer(serializers.Serializer):
times = serializers.ListField(child=serializers.CharField())
airmasses = serializers.ListField(child=serializers.FloatField())
class SiteAirmassSerializer(serializers.Serializer):
site = DictField(child=SiteAirmassDatumSerializer(), source='*')
class AirmassSerializer(serializers.Serializer):
airmass_data = DictField(child=SiteAirmassSerializer(), source='*')
airmass_limit = serializers.FloatField()
When attempting to validate the aforementioned structure, I get the following error:
{
"airmass_data": {
"site-A": {
"site": [
"This field is required."
]
}
}
}