django rest framework change primary key to use a unqiue field

后端 未结 3 1607
情话喂你
情话喂你 2021-02-07 06:48

I have a model which is called GameProfile, which is a one to one relation with User model. I used HyperlinkedModelSerializer across all m

3条回答
  •  孤独总比滥情好
    2021-02-07 07:28

    If I'm understanding your question correctly, you want a url structure like so:

    /api//
    

    If that is the case, you should checkout the lookup_field option. Link

    You're Serializer class would look something like:

    class GameProfileSerializer(serializers.HyperlinkedModelSerializer):
        """ 
        """
        user_pk = serializers.Field(source='user.id')
    
        class Meta:
            model = GameProfile
            lookup_field = 'user_pk'  # Might have to use 'user__id'
    

提交回复
热议问题