[Solved]-How to append extra data to the existing serializer in django

21👍

You can use SerializerMethodField to add extra data to the serialized representation of an object.

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.

In your serializer, add Available and Assign SerializerMethod fields. Doing this will always add Available and Assign keys in your serialized data.

class getEmpDcSerializer(serializers.ModelSerializer): 
    Available = serializers.SerializerMethodField() # add field
    Assign = serializers.SerializerMethodField() # add field

    class Meta:
        model = emp_details
        fields = ('emp_id','emp_dc_id','emp_first_name','emp_last_name','emp_role_id', 'Available', 'Assign')

    def get_Available(self, obj):
        # here write the logic to compute the value based on object
        return 1

    def get_Assign(self, obj):
        # here write the logic to compute the value based on object
        return 2

Leave a comment