I am a newbie to django rest framework and have created a sample Employee
model.
My models.py:
class Employees(models.Model
SerializerMethodField works fine, and we can also store data in serializer object and let method get_field_name
use that.
Example:
class MySerializer(serializers.ModelSerializer):
statistic = serializers.SerializerMethodField()
def __init__(self, instance=None, data=serializers.empty, statistic=None, **kwargs):
super(MySerializer, self).__init__(instance=instance, data=data, **kwargs)
self.statistic = statistic
def get_statistic(self, obj):
if self.statistic is None:
return serializers.empty
return self.statistic.get(obj.id, {})