我发现此解决方案是最简单的。
from collections import OrderedDictfrom rest_framework import serializersclass NonNullModelSerializer(serializers.ModelSerializer): def to_representation(self, instance): result = super(NonNullModelSerializer, self).to_representation(instance) return OrderedDict([(key, result[key]) for key in result if result[key] is not None])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)