serializers.py
class BusOncomingSerializer(serializers.Serializer):
idn = serializers.IntegerField(read_only=True)
stops_left = serializers.IntegerField(read_only=True)
class StopOncomingSerialier(serializers.Serializer):
idn = serializers.IntegerField(read_only=True)
buses = BusOncomingSerializer(many=True)
I have my serializer instances which are valid in a list as below:
buses_serializers = [bus1, bus2, bus3]
# busX means instance of BusOncomingSerializer and they are valid.
I am trying to add these BusOncomingSerializer
instances to a single StopOncomingSerializer
instance by:
serializer = serializers.StopOncomingSerializers(data={"idn": 1, "buses": buses_serializers})
And I run is_valid()
. However, it raises:
TypeError: BusOncomingSerializer(data={'idn': 285, 'stops_left': 7}): idn = IntegerField(read_only=True) stops_left = IntegerField(read_only=True) is not JSON serializable
Should I not add my serializer instances with a list, is there another way?
Environment
- python 3.5.1
- django 1.9.5
- django-rest-framework 3.3.3
You are passing the data incorrectly to
StopOncomingSerializer
. Instead of passingbus
instances, you should pass the values as list of dictionaries.Sample Data format:
On Python Shell: