本文介绍了从Django Rest Framework序列化器中排除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下序列化程序中,我有一个嵌套的序列化程序[ ContainerSerializer
]字段,我想从(容器) ContainerSerializer
中排除一个字段,但是我什么都不想要在 ContainerSerializer
中进行更改.我该怎么办?
In the following serializer, I have a nested serializer [ContainerSerializer
] field and I want to exclude a field from (container)ContainerSerializer
but I don't want any change in ContainerSerializer
. How can I do that?
class BLcontainerMergedSerializer(serializers.ModelSerializer):
container = ContainerSerializer()
class Meta:
model = BLcontainer
推荐答案
创建另一个序列化器,例如 BLContainerSerializer
并排除其中的字段.然后在 BLcontainerMergedSerializer
中使用它.希望这会有所帮助.
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
这篇关于从Django Rest Framework序列化器中排除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!