what's different about list_route and detail_route in django-rest-framework?

后端 未结 3 1565
旧时难觅i
旧时难觅i 2021-02-20 18:06

like title,
what\'s different about list_route and detail_route in django-rest-framework?
if I want to get 1 in url xxx/book

3条回答
  •  独厮守ぢ
    2021-02-20 18:37

    **Read this and definitely you will get the difference and how to use it **

    If we have a ad hoc method (for e.g. current method which is in the same viewset that we have used for different methods, basically ad-hoc means 'this'), we can use custom route for this method, we can define our own url above the method inside the @list_route and @detail_route decorator

    The difference between the @list_route and @detail_route is the @detail_route decorator contains pk in its URL pattern and is intended for methods which require a single instance. The @list_route decorator is intended for methods which operate on a list of objects (list of records)

    Get reference through enter link description here

    For example

    **It will hit to the same url at the url.py but for @list_raoute we have append /reset-user-password/ which we have mention on @list_route to the url when we call it.(e.g 
    
    /// In url.py**
    
    router = routers.DefaultRouter()
    
    router.register(r'register', api_views.UserProfileViewSet,  base_name="userprofileviewset")
    urlpatterns = [
        url(r'^api/v1/', include(router.urls)),
    ]
    

    **////// In API call or in url call

    for create user**
    http://127.0.0.1:8000/api/v1/register/
    

    ### forget password

    http://127.0.0.1:8000/api/v1/resister/reset-user-password/
    
    )
    
    class UserProfileViewSet(viewsets.ViewSet):
        """
        IT's use to create new user(auth user). accepted method is post. 
        end point /register
        """
        permission_classes = (AllowAny,)
        serializer_class = UserProfileSerializer
    
    """
    It gives the list of all users
    """
    def list(self, request):
        queryset = UserProfile.objects.all()
        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)
    
    """
    It creates new user
    """
    def create(self, request):
        serializer = self.serializer_class(data=request.data)
        # check email address is exists or not.
        user_type = request.data['user_type']
        user_token = register_by_social(request.data['email'], request.data['username'], user_type)
        
        if not user_token or user_token == True:
            if not User.objects.filter(Q(email=request.data['email']) 
                | Q(username=request.data['username'])).exists():
    
                if serializer.is_valid():
                    userprofile = serializer.save()
    
                    return Response({
                        'status': status.HTTP_201_CREATED,
                        'message': 'Successfully signup new user.',
                        'token': userprofile.user.auth_token.key })
    
                return Response({
                    'status': status.HTTP_400_BAD_REQUEST,
                    'message': 'Please provided required fields.',
                    'error' : serializer.errors })
    
            return Response({
                'status': status.HTTP_409_CONFLICT,
                'message': 'Email address or username is already exists.'})
    
        return Response({
            'status': status.HTTP_200_OK,
            'message': 'Social user is already registered.',
            'token': user_token })
    
    
    @list_route(permission_classes=[IsAuthenticated], authentication_classes = (BasicAuthentication, TokenAuthentication), 
            methods=['post'], url_path='reset-user-password')
    def reset_user_password(self, request, pk=None):
        """
        It resets the user password
        """
        reset_password_serializer = UserResetPasswordSerializer(request.user, data=request.data)
        
        if reset_password_serializer.is_valid():
    
            if not request.user.check_password(request.data.get('password')):
                return Response({
                    'status': status.HTTP_400_BAD_REQUEST,
                    'message': 'Password id wrong, please enter correct password',
                    })
    
            request.user.set_password(request.data.get('new_password'))
            request.user.save()
            return Response({
                    'status': status.HTTP_201_CREATED,
                    'message': 'Password updated successfully',
                    })
    
    
    class PlayListViewSet(viewsets.ViewSet):
        permission_classes = (IsAuthenticated,)
        serializer_class = PlayListSerializer
        serializer_add_playlist_class = LikeContentSerializer
    
        @detail_route(methods=['post'], url_path='add-content-to-playlist')
        def add_playlist(self, request, pk=None):
         
            serializer = self.serializer_add_playlist_class(data=request.data)
            playlist = PlayList.objects.filter(id=pk)
            if serializer.is_valid():
                content_type = request.data['content_type']
    
                if content_type =="audio":  
                    content = Song.objects.filter(id=request.data['content'])
                    playlist[0].songs.add(content[0])
                    playlist[0].save()
                    
                 
                if content_type =="video":
                    content = Video.objects.filter(id=request.data['content'])
                    playlist[0].videos.add(content[0])
                    playlist[0].save()
                
                if content_type =="youtube":
                    content = YouTubeVideo.objects.filter(id=request.data['content'])
                    playlist[0].youtbue_videos.add(content[0])
                    playlist[0].save()
    
                return Response({
                    'status': status.HTTP_201_CREATED,
                    'message': 'Successfully playlist updated'})
    
            return Response({
                'status': status.HTTP_400_BAD_REQUEST,
                'message': 'Data is not valid, try again',
                'error' : serializer.errors })
    

提交回复
热议问题