What is different from list_route and detail_route in django-rest-framework?

as a name,
what is different from list_routeand detail_routein django-rest-framework?
if I want to get 1in url xxx/books/1/,
how can I write url.pyand views.py?

+4
source share
3 answers

@list_routeand @detail_route- additional actions that we can add to ViewSet. Both provide a custom dialing routing tool. Any methods on ViewSet, decorated @detail_routeor @list_route, will also be redirected. list_routewill provide all related records, and detail_routewill provide only a specific record. For example, given a method like this in the UserViewSet class:

class UserViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
    def set_password(self, request, pk=None):

Next, the following URL pattern will be generated:

URL pattern: ^users/{pk}/set_password/$ Name: 'user-set-password'

For more information about routers, you can visit the official Django Rest Rramewrok documentation on routers .

If you want to get xxx/books/1/, then your url.pyand views.pyshould look like.

urls.py:

url(r'^xxx/books/(?P<id>[0-9]+)$', views.myview)

views.py:

@csrf_exempt
def myview(request , id):
+5
source

detail_route, . , , , , , . {}/{}/

: django drf router doc

1 - :

class ParkingViewSet(viewsets.ModelViewSet):
serializer_class = BookSerializer

def retrieve(self, request, pk=None):
    # Here pk will be 1.
    queryset = Book.objects.get(pk=pk)         
    serializer = BookSerializer(queryset)                         
    return Response({'msg':"",'data':serializer.data, 'status':'OK'})

xxx - , url_path URL- . - :

@detail_route(methods=['get'], url_path='(books/?P<num>\d+)')

num parmeter

urls.py, :

from django.conf.urls import url, include                                        
from recharge_card import views                                                  
from rest_framework.routers import DefaultRouter                                 

# Create a router and register our viewsets with it.                             
router = DefaultRouter()                                                         
router.register(r'xxx', views.XxxViewSet, base_name="xxx")  

urlpatterns = [  
    url(r'^api/', include(router.urls)),                                         
]                                                                                
0

** , **

(, , , , ad-hoc 'this'), , url @list_route @detail_route decorator

@list_route @detail_route - @detail_route, pk URL , . @list_route , ( )

**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)),
]

**////// API URL

**
http://127.0.0.1:8000/api/v1/register/

### forreset password

http://127.0.0.1:8000/api/v1/resister/reset-user-password/

)

class UserProfileViewSet (viewsets.ViewSet): "" Using IT to create a new user (auth user). accepted post method. endpoint / 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 })
0
source

Source: https://habr.com/ru/post/1617016/


All Articles