[Answered ]-Filter nested serializer model field (exclude particular field)

1👍

You can try like this using SerializerMethodField:

class BlogSerializer(serializers.ModelSerializer):
    comment = serializers.SerializerMethodField()

    def get_comment(self, instance):
        comment_instances = instance.comment.exclude(completed=True)
        return BlogCommentSerializer(comment_instances , many=True).data
👤ruddra

0👍

You should use SerializerMethodField, but use query filtering, and then pass the data to the BlogCommentSerializer serializer

class BlogSerializer(serializers.ModelSerializer):
    comment = serializers.SerializerMethodField()

    def get_comment(self, obj):
        queryset = Comment.objects.
            filter(blog=obj).exclude(blog__completed=True)
        return [BlogCommentSerializer(q).data for q in queryset]

Leave a comment