Django-rest-framework serializer to_representation
I have ModelSerializer
c SerializerMethodField
. I want to override the to_representation
serializer method to have custom output, but I don't know how to access SerializerMethodField
:
class MySerializer(serializers.ModelSerializer):
duration = serializers.SerializerMethodField()
def get_duration(self, obj):
return obj.time * 1000
def to_representation(self, instance):
return {
'name': instance.name,
'duration of cycle': # HOW TO ACCESS DURATION????
}
class Meta:
model = MyModel
+3
source to share