serializers.py
class ThreadSerializer(serializers.ModelSerializer):
author = UserSerializer(read_only=True)
last_post_user = UserSerializer(read_only=True)
class Meta:
model = Thread
fields = '__all__'
read_only_fields = ('locked', 'views', 'post_count', 'last_post_time')
class ChannelSerializer(serializers.ModelSerializer):
threads = ThreadSerializer(many=True, read_only=True)
class Meta:
model = Channel
fields = ['id', 'name', 'description', 'category', 'threads']
class CategorySerializer(serializers.ModelSerializer):
channels = ChannelSerializer(many=True, read_only=True)
class Meta:
model = Category
fields = ['id', 'name', 'channels']
You'll notice I have nested ChannelSerializer in CategorySerializer, when I visit the endpoint that queries data using CategorySerializer I was hoping it would list out only data from ChannelSerializer however it lists data from ThreadSerializer as well, I'd assume this is expect behavior since ThreadSerializer is nested inside ChannelSerializer.
Is there some elegant way I can exclude fields when I nest serializers? in this case I'd like to exclude threads from appearing when querying data using the CategorySerializer
I tried the answer from a similar question but can't seem to get it working in my case unfortunately, it ends up listing the threads again, also I'd think there's a simpler way to do it.
