Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework How to update SerializerMethodField

Tags:

I have a serializer like this:

class PersonSerializer(serializers.ModelSerializer):
    gender = serializers.SerializerMethodField()
    bio = BioSerializer()

    class Meta:
        model = Person
        fields = UserSerializer.Meta.fields + ('gender', 'bio',)

    def get_gender(self, obj):
        return obj.get_gender_display()

I used this to display "Male" and "Female"(insted of "M" of "F") while performing GET request.

This works fine.

But now I am writing an patch method for the model and SerializerMethodField() has read_only=True. So I am not getting value passed for gender field in serializer.validated_data(). How to overcome this issue?

like image 837
Kishan Mehta Avatar asked May 27 '16 05:05

Kishan Mehta


People also ask

How do you pass extra context data to Serializers in Django REST framework?

In function based views we can pass extra context to serializer with "context" parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with "self. context". From example, to get "exclude_email_list" we just used code 'exclude_email_list = self.

What is SerializerMethodField?

SerializerMethodField. This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object.

What is Read_only true in Django?

Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.


2 Answers

So if I understand you correctly, you want to send {'gender': 'Male'} in your PATCH request.

Therefor, you have to tell your serializer how to convert your representation i.e. 'Male' into the internal value.

As you can see in source, SerializerMethodField only covers the conversion from internal value to the representation.

You can implement a custom SerializerField that performs the necessary conversions. A naive implementation could something like this:

class GenderSerializerField(serializers.Field):

    VALUE_MAP = {
        'M': 'Male',
        'F': 'Female'
    }

    def to_representation(self, obj):
        return self.VALUE_MAP[obj]            

    def to_internal_value(self, data):
        return {k:v for v,k in self.VALUE_MAP.items()}[data]

class PersonSerializer(serializers.ModelSerializer):
    gender = GenderSerializerField()
    ...

Note that this untested and lacks any validation, check out the DRF docs on custom fields.

like image 119
Daniel Hepper Avatar answered Sep 25 '22 01:09

Daniel Hepper


Aside from accepted answer, there can be other simpler hooks. If 'create' and 'update' worked as you wanted before modifiying gender field, then you can do as follow to get everything to default for create and update requests.

  • Do not user SerializerMethodField. Instead override serializer representation.
class PersonSerializer(serializers.ModelSerializer):
    bio = BioSerializer()
    
    class Meta:
        model = Person
        fields = UserSerializer.Meta.fields + ('bio',)


    def to_representation(self, obj):
        ret = super().to_representation(obj)
        ret['gender'] = obj.get_gender_display()
        return ret

  • Override the __init__ method . .
class PersonSerializer(serializers.ModelSerializer):
    bio = BioSerializer()
    
     def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        try:
            if self.context['request'].method in ['GET']:
                self.fields['gender'] = serializers.SerializerMethodField()
        except KeyError:
            pass

    class Meta:
        model = Person
        fields = UserSerializer.Meta.fields + ('bio',)

    def get_gender(self, obj):
        return obj.get_gender_display()   

like image 27
Sagar Adhikari Avatar answered Sep 23 '22 01:09

Sagar Adhikari