class SampleSerializer(serializers.HyperlinkedModelSerializer):
# creator = serializers.ReadOnlyField(source='creator.username')
# serializer 变换中 '__all__' 没有 id 属性, 手动添加此属性, 以适应 __all__ 达到偷懒的效果
id = serializers.SerializerMethodField()
def get_id(self, instance):
return instance.id
class Meta:
model = Sample
fields = '__all__'
网友评论