id field in django rest framework serializer

前端 未结 3 1863
梦毁少年i
梦毁少年i 2020-12-14 02:35

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

class PackageSerializer(serializers.HyperlinkedModelSerializer):

    class         


        
3条回答
  •  感动是毒
    2020-12-14 03:12

    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.

提交回复
热议问题