[Answer]-Relational condition check in Django Rest Framework

1👍

Why not overriding validate() on serializer? See Object-level validation

class WorkloadPutSerializer(serializers.Serializer):

    vdi = VdiSerializer(required = False)
    db = DbSerializer(required = False)
    pk = serializers.IntegerField(read_only=True)

    def validate(self, data):
        """
        Check if at least one of data['vdi'] or data['vm']
        exists and then return the data. Otherwise raise 
        ValidationError. 
        """
        if data['vdi'] or data['vm']:
            return data
        else:
            raise serializers.ValidationError("No No No")
👤Sam R.

Leave a comment