Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST Framework - Serializing optional fields

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.

like image 817
Aziz Alfoudari Avatar asked Jul 09 '13 15:07

Aziz Alfoudari


2 Answers

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:

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('code',)

    def __init__(self, *args, **kwargs):
        if SHOW_CLASSIFICATION: # add logic here for optional viewing
            self.Meta.fields = list(self.Meta.fields)
            self.Meta.fields.append('classification')
        super(ProductSerializer, self).__init__(*args, **kwargs)

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:

class ProductSerializer(serializers.Serializer):
    ...
    classification = serializers.SerializerMethodField('get_classification')

    def get_classification(self, obj):
        return getattr(obj, 'classification', None)

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.

def get_serializer_class(self):
    """ An authenticated user looking at their own user object gets more data """
    if self.get_object() == self.request.user:
        return SelfUserSerializer
    return UserSerializer

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 like

def remove_fields_from_representation(self, representation, remove_fields):
    """ Removes fields from representation of instance.  Call from
    .to_representation() to apply field-level security.
    * remove_fields: a list of fields to remove
    """
    for remove_field in remove_fields:
        try:
            representation.pop(remove_field)
        except KeyError:
            # Ignore missing key -- a child serializer could inherit a "to_representation" method
            # from its parent serializer that applies security to a field not present on
            # the child serializer.
            pass

and then in your serializer, call that method like

def to_representation(self, instance):
    """ Apply field level security by removing fields for unauthorized users"""
    representation = super(ProductSerializer, self).to_representation(instance)
    if not permission_granted: # REPLACE WITH PERMISSION LOGIC
        remove_fields = ('classification', ) 
        self.remove_fields_from_representation(representation, remove_fields)
    return representation

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.

like image 110
Mark Chackerian Avatar answered Oct 16 '22 10:10

Mark Chackerian


The method describe below did the work for me. Pretty simple,easy and worked for me.

DRF version used = djangorestframework (3.1.0)

class test(serializers.Serializer):
  id= serializers.IntegerField()
  name=serializers.CharField(required=False,default='some_default_value')
like image 42
RAJ GUPTA Avatar answered Oct 16 '22 10:10

RAJ GUPTA