Get object by field other than primary key

前端 未结 5 1035
眼角桃花
眼角桃花 2020-12-30 09:52

Hi I\'m new to both Django and the Django-Rest-Framework. I\'ve gone through the tutorials. What I\'m trying to do (as a learning exercise) is return an object based off a f

5条回答
  •  失恋的感觉
    2020-12-30 10:07

    Credit https://www.youtube.com/watch?v=dWZB_F32BDg

    Use lookup_field to define the field used for querying the table and look_up_kwargs for the field in the url

    url(r'^videos/(?P[^/]+)/$', video_detail)

    class VideoDetailView(viewsets.ModelViewSet):
        serializer_class = VideosSerializer
        queryset = Videos.objects.all()
        lookup_field = 'videoName'
        lookup_url_kwarg = 'videoName'
    

提交回复
热议问题