Exclude a field from django rest framework serializer

前端 未结 2 734
你的背包
你的背包 2021-02-11 15:24

In the following serializer, I have a nested serializer [ContainerSerializer] field and I want to exclude a field from (container)ContainerSerializer b

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-11 15:57

    Create another serializer say BLContainerSerializer and exclude fields there. Then use this in your BLcontainerMergedSerializer. Hope this helps.

    class BLContainerSerializer(serializers.ModelSerializer):
        class Meta:
            model = Container
            exclude = ('field1', )
    
    
    class BLcontainerMergedSerializer(serializers.ModelSerializer):
        container = BLContainerSerializer()
    
        class Meta:
            model = BLcontainer
    

提交回复
热议问题