Reputation: 19
In my nested serializer i want to show only movie name and exclude the other fields
[
{
"id": 2,
"watchlist": [
{
"id": 3,
"platform": "Netflix",
"title": "Martian",
"storyline": "A lost astronaut of Mars survived",
"average_rating": 4.1,
"total_rating": 2,
"active": true,
"created": "2022-04-05T05:37:35.902464Z"
},
{
"id": 4,
"platform": "Netflix",
"title": "Intersteller",
"storyline": "Finding new home",
"average_rating": 0.0,
"total_rating": 0,
"active": true,
"created": "2022-04-06T04:52:04.665202Z"
},
{
"id": 5,
"platform": "Netflix",
"title": "Shutter Island",
"storyline": "Psycopath",
"average_rating": 0.0,
"total_rating": 0,
"active": true,
"created": "2022-04-06T04:52:51.626397Z"
}
],
"platform": "Netflix",
"about": "streaming, series and many more",
"website": "https://www.netflix.com"
},
]
In the above data, "watchlist" is the nested serializer data i want to show only "title" and exclude all other data
I have included WatchListSerializer class as "nested" serializer in the StreamPlatformSerializer class. I want that on "title should be shown, rest other fields should be excluded from nested serializer part"
below is the code...
class WatchListSerializer(serializers.ModelSerializer):
# reviews = ReviewSerializer(many=True, read_only=True)
platform = serializers.CharField(source='platform.platform')
class Meta:
model = WatchList
fields = '__all__'
# def to_representation(self, value):
# return value.title
class StreamPlatformSerializer(serializers.ModelSerializer):
watchlist = WatchListSerializer(many=True, read_only=True)
# watchlist = serializers.CharField(source='watchlist.title')
class Meta:
model = StreamPlatform
fields = '__all__'
after removing other fields it should look like this as below..
[
{
"id": 2,
"watchlist": [
{
"title": "Martian"
},
{
"title": "Intersteller",
},
{
"title": "Shutter Island",
],
"platform": "Netflix",
"about": "streaming, series and many more",
"website": "https://www.netflix.com"
},
]
Upvotes: 0
Views: 66
Reputation: 3233
There may be two approaches for this thing First Approach:
class WatchListSerializer(serializers.ModelSerializer):
# reviews = ReviewSerializer(many=True, read_only=True)
class Meta:
model = WatchList
fields = ("title",)
# def to_representation(self, value):
# return value.title
class StreamPlatformSerializer(serializers.ModelSerializer):
watchlist = WatchListSerializer(many=True, read_only=True)
# watchlist = serializers.CharField(source='watchlist.title')
class Meta:
model = StreamPlatform
fields = '__all__'
With this approach the downside will be that WatchListSerializer
can only be used in this serializer not as a standalone serializer.
For second approach I need to see your models. It would be like
class StreamPlatformSerializer(serializers.ModelSerializer):
watchlist = serializers.SerializerMethodField("get_watchlist")
class Meta:
model = StreamPlatform
fields = '__all__' # include watchlist as well
def get_watchlist(self, obj):
return obj.watchlist.all().values('title')
I like this approach personally.
Upvotes: 1