def to_python(value): try: return ObjectId(base64_decode(value)) except (InvalidId, ValueError, TypeError): raise ValidationError()