Exclude a field from django rest framework serializer

前端 未结 2 1908
猫巷女王i
猫巷女王i 2021-02-11 14:54

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:42

    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
    
    0 讨论(0)
  • 2021-02-11 15:43

    There is a fields meta property:

    class BLcontainerMergedSerializer(serializers.ModelSerializer):
        container = ContainerSerializer()
        class Meta:
            model = BLcontainer
            fields = ('field1', 'field2')
    

    Reference: Django REST docs

    0 讨论(0)
提交回复
热议问题