Filtering serializer response data
<p>I have a ManyToMany relation with tag and items:</p>

<pre><code>class Tag(BaseModel):
    name = models.CharField(max_length=255)  # ToDo Change max length
    description = models.TextField(blank=True, null=True)


class Item(BaseModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    image = models.ImageField(upload_to='items', blank=True)
    title = models.TextField(blank=False, null=True)
    message = models.TextField(blank=True, null=True)
    fav_count = models.IntegerField(default=0)
    tags = models.ManyToManyField(Tag, related_name='tags')
</code></pre>

<p>I need all fields to be serialized, but i wish to only limit the response values. 
Example:</p>

<p>What I'm receiving now:</p>

<pre><code>{
    "user": 2,
    "image": null,
    "title": "test3",
    "message": "testmessage",
    "fav_count": 0,
    "tags": [
        {
            "id": 7,
            "name": "tag1",
            "description": null
        },
        {
            "id": 8,
            "name": "tag2",
            "description": null
        }
    ]
}
</code></pre>

<p>But i only wish to receive the tag ids not the name and description...</p>

<p>My simple view:</p>

<pre><code>if request.method == 'GET':
        items = Item.objects.all()
        serializer = ItemSerializer(items, many=True)
        return Response(serializer.data)
</code></pre>

<p>Would i need to rebuild my response data to include/exclude or is there a better way to do this? (or if iv missed the terminology)</p>

<p>use <code>PrimaryKeyRelatedField</code> DRF field in your serializer</p>

<p>Example</p>

<pre><code>class ItemSerializer(serializers.ModelSerializer):
    tags = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = Item
        fields = ('tags', 'image',.....other fields)
</code></pre>

<p>Response</p>

<pre><code>{
    'image': 'image1',
    ...........
    'tags': [
        89,
        90,
        91,
        ...
    ]
    ..........
}
</code></pre>

<p>In you want to do it dynamically based on a request parameter. </p>

<pre><code>   class ItemSerializer(serializers.ModelSerializer):
        tags = serializers.SerializerMethodField()

   def get_tags(self, obj):
       if self.request.get('some_condition'):
           data_tags = TagSerializer(obj.tags, many=True).data
           data = map(data.pop('field_to_remove') for data in data_tags)
           return list(data)
       else:
           return TagSerializer(obj.tags, many=True).data
</code></pre>

<p>Then, pass request to your serializer when you init it in your view.</p>

<pre><code>serializer = ItemSerializer(data, context={'request':self.request})
</code></pre>

python django django-rest-framework