4👍
✅
It is a common problem, every time when you want to change representation behavior you are free to use to_representation
method of a serializer:
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Provider
fields = ('pk', 'title', 'slug', 'body', 'category')
def to_representation(self, instance):
representation = super(ArticleSerializer, self).to_representation(instance)
representation['category'] = CategorySerializer(instance.category).data
return representation
Source:stackexchange.com