Routers

Other topics

[Introductory] Basic usage, SimpleRouter

Automatic routing for the DRF, can be achieved for the ViewSet classes.

  1. Assume that the ViewSet class goes by the name of MyViewSet for this example.

  2. To generate the routing of MyViewSet, the SimpleRouter will be utilized.
    On myapp/urls.py:

    from rest_framework import routers
    
    router = routers.SimpleRouter()        # initialize the router.
    router.register(r'myview', MyViewSet)  # register MyViewSet to the router.
    
  3. That will generate the following URL patterns for MyViewSet:

    • ^myview/$ with name myview-list.
    • ^myview/{pk}/$ with name myview-detail
  4. Finally to add the generated patterns in the myapp's URL patterns, the django's include() will be used.
    On myapp/urls.py:

    from django.conf.urls import url, include
    from rest_framework import routers
    
    router = routers.SimpleRouter()
    router.register(r'myview', MyViewSet)
    
    urlpatterns = [
        url(r'other/prefix/if/needed/', include(router.urls)),
    ]
    

Syntax:

  • router = routers.SimpleRouter()
  • router.register(prefix, viewset)
  • router.urls # the generated set of urls for the registered viewset.

Contributors

Topic Id: 10938

Example Ids: 32731

This site is not affiliated with any of the contributors.