HyperlinkedModelSerializer
默认情况下不包括id
。在 2.2 及更早版本中,您需要将其显式添加为字段...
class PackageSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.Field()
class Meta:
model = Package
fields = ('id', 'url', 'title','location')
从 2.3 开始,您可以简单地将 'id' 添加到fields
选项中......
class PackageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Package
fields = ('id', 'url', 'title','location')
从 3.x (< 3.3) 开始,如果要显式添加它而不使用选项,则必须使用ReadOnlyField()
而不是...Field()
fields
class PackageSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = Package