I'm new to Django REST Framework and searching for a method to serialize a list of dictionaries.
Target data is a time-series. Each dictionary contains hourly value and each list contains hourly values of a day.
class Hourly:
hour = None
value = None
def __init__(self, hour, value):
self.hour = hour
self.value = value
class Daily:
date = None
vlist = None
def __init__(self, date, vlist):
self.date = date
self.vlist = vlist
It is easy to create a serializer for the hourly class by following the Django REST Framework API guide.
class HourlySerializer(serializers.Serializer):
hour = serializers.IntegerField(required=True, min_value=0, max_value=23)
value = serializers.FloatField(required=True)
def create(self, validated_data):
return Hourly(**validated_data)
def update(self, instance, validated_data):
instance.hour = validated_data.get('hour', instance.hour)
instance.value = validated_data.get('value', instance.value)
return instance
My objective is simple: creating a serializer for the daily data. (1) The serializer must restrict the length of data as 24. (2) It should have an ability to validate each hourly data.
#1. Using ListField
In case of restricting the data length, ListField has arguments min_length and max_length #.
I think this kind of code may be possible #, however, the API guide does not say anything about defining the child of a ListField as a serializer.
class DailySerializer(serializers.Serializer):
date = serializers.DateField(required=True)
vlist = serializers.ListField(child=HourlySerializer(), min_length=24, max_length=24)
#2. Using ListSerializer
The API guide says one can nest serializers just like they are fields. If the nested value is a list of items, many=True can be used to initiate ListSerializer. However, there is no argument about restricting the data length.
class DailySerializer(serializers.Serializer):
date = serializers.DateField(required=True)
vlist = HourlySerializer(many=True)
Here are my questions:
- Is the method using
ListFieldis possible with no problem? Or, can I passmin_lengthandmax_lengtharguments withmany=True? - The API guide does not say anything about defining
create()andupdate()forListFieldorListSerializer. There are only cases for a nested serializer withoutmanyoption # and aListSerializerwithout non-list field #. Is there any example about this?