Remove specific instance when serializing multiple objects (many=True) and one of them has an error

Viewed 97

I've a serializer similar to this:

class ExampleSerializer(serializers.ModelSerializer):
    attr3 = serializers.SerializerMethodField()

    class Meta:
        model = ModelExample
        fields = ["attr1", "attr2", "attr3"]

    def get_atrr3(self, instance):
        try:
            # Do something
        except Exception:
            raise

I'm a serializing a list of objects.

ExampleSerializer(object_list, many=True).data

Is there a way to exclude instances raising errors when serializing.. so I still get the rest of the correct object in the list serialized anyways?

Btw, I can't pre-filter the object_list in order to exclude the problematic items.

0 Answers
Related