I have an object that has optional fields. I have defined my serializer this way:
code = serializers.Field(source="Code")
classification = serializers.CharField(source="Classification", required=False)
'Product' object has no attribute 'Classification'
The serializers are deliberately designed to use a fixed set of fields so you wouldn't easily be able to optionally drop out one of the keys.
You could use a SerializerMethodField to either return the field value or
None if the field doesn't exist, or you could not use serializers at all and simply write a view that returns the response directly.
Update for REST framework 3.0
serializer.fields can be modified on an instantiated serializer. When dynamic serializer classes are required I'd probably suggest altering the fields in a custom