How to dynamically remove fields from serializer output
<p>I'm developing an API with Django Rest framework, and I would like to dynamically remove the fields from a serializer. The problem is that I need to remove them depending on the value of another field. How could I do that?
I have a serializer like:</p>

<pre><code>class DynamicSerliazer(serializers.ModelSerializer):
    type = serializers.SerializerMethodField()
    url = serializers.SerializerMethodField()
    title = serializers.SerializerMethodField()
    elements = serializers.SerializerMethodField()

    def __init__(self, *args, **kwargs):
        super(DynamicSerliazer, self).__init__(*args, **kwargs)
        if self.fields and is_mobile_platform(self.context.get('request', None)) and "url" in self.fields:
            self.fields.pop("url")
</code></pre>

<p>As you can see, I'm already removing the field "url" depending whether the request has been done from a mobile platform. But, I would like to remove the "elements" field depending on the "type" value. How should I do that?</p>

<p>Thanks in advance</p>