Django Rest Framework ListField and DictField

前端 未结 1 1351
生来不讨喜
生来不讨喜 2020-12-10 02:40

I am having struggle with understanding ListField and DictField. I want to use it as a field on a serializer. I have a ListField which

相关标签:
1条回答
  • 2020-12-10 03:02

    I think instead of doing that, you should use nested serializers.

    Create a BusSerializer having fields idn and stops_left. Then include this serializer in your StopOncomingSerializer as buses field with many=True argument to handle multiple buses data.

    class BusSerializer(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 = BusSerializer(many=True)
    
    0 讨论(0)
提交回复
热议问题