I have an object that has optional fields. I have defined my serializer this way:
class ProductSerializer(serializers.Serializer):
code = serializers.Field(source="Code")
classification = serializers.CharField(source="Classification", required=False)
I thought required=False
would do the job of bypassing the field if it doesn't exist. However, it is mentioned in the documentation that this affects deserialization rather than serialization.
I'm getting the following error:
'Product' object has no attribute 'Classification'
Which is happening when I try to access .data
of the serialized instance. (Doesn't this mean it's deserialization that's raising this?)
This happens for instances that do not have Classification
. If I omit Classification
from the serializer class it works just fine.
How do I correctly do this? Serialize an object with optional fields, that is.
Django REST Framework 3.0+
Dynamic fields now supported, see http://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields -- this approach defines all of the fields in the serializer, and then allows you to selectively remove the ones you don't want.
Or you could also do something like this for a Model Serializer, where you mess around with Meta.fields in the serializer init:
You'd have to ask Tom though if this is the "correct way" since it may not fit in with the long term plan.
Django REST Framework < 3.0
Try something like this:
Multiple Serializers
Another approach would be to create multiple serializers with different sets of fields. One serializer inherits from another and adds additional fields. Then you can choose the appropriate serializer in the view with the
get_serializer_class
method. Here's an actual example of how I use this approach to call different serializers to present different user data if the user object is the same as the request user.Removing fields from representation
Another approach that I've used in security contexts is to remove fields in the
to_representation
method. Define a method likeand then in your serializer, call that method like
This approach is straightforward and flexible, but it comes at the cost of serializing fields that are sometimes not displayed. But that's probably okay.
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 customSerializer.__init__()
method.From the "it's a terrible hack relying on specific implementation details of both DRF and Django, but it works (at least for now)" files, here's the approach I used to include some additional debugging data in the response from a "create" method implementation on a serializer:
This is a temporary approach that lets me use the browsable API to display some of the raw response data received from a particular remote service during the creation process. In the future, I'm inclined to keep this capability, but hide it behind a "report debugging info" flag in the creation request rather than returning the lower level info by default.
For this purpose the serializers have the
partial
argument. If when the serializer is initialized you can passpartial=True
. If you are using generics or mixins you can overrider theget_serializer
function as follows:And that will do the trick.
Note: This allows all fields to be optional and not only a specific one. If you want only specifics, you can override the method (i.e. update) and add validations of existence for various fields.
The method describe below did the work for me. Pretty simple,easy and worked for me.
DRF version used = djangorestframework (3.1.0)