id field in django rest framework serializer

2020-02-08 07:24发布

I'm using django rest framework and just creating a simple serializer like this:

class PackageSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Package
        fields = ('id', 'url', 'title','location')

However I get this error:

KeyError at /cerberus/packages/
'id'

How come I can't get the primary key 'id' as part of my serialized data? Thanks for helping!

3条回答
贪生不怕死
2楼-- · 2020-02-08 08:05

According to the Django docs:

The default implicitly-generated id field is marked as read_only. This causes it to be removed on updates.

Thus, if you need the id field in an update method, you must not declare it as read-only but use a read-write field instead:

class PackageSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()
查看更多
Emotional °昔
3楼-- · 2020-02-08 08:09

I just tweaked this to make it a little more pluggable by creating this class:

class HyperlinkedModelSerializerWithId(serializers.HyperlinkedModelSerializer):
    """Extend the HyperlinkedModelSerializer to add IDs as well for the best of
    both worlds.
    """
    id = serializers.ReadOnlyField()

Then, just use that to make your serializer and you're on your way.

查看更多
贼婆χ
4楼-- · 2020-02-08 08:18

HyperlinkedModelSerializer doesn't include the id by default. In 2.2 and earlier you'll need to add it explicitly as a field...

class PackageSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.Field()

    class Meta:
        model = Package
        fields = ('id', 'url', 'title','location')

From 2.3 onwards, you can simply add 'id' to the fields option...

class PackageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Package
        fields = ('id', 'url', 'title','location')

From 3.x onwards, you must use ReadOnlyField() instead of Field() if you want to add it explicitly and not use the fields option...

class PackageSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()

    class Meta:
        model = Package
查看更多
登录 后发表回答