Understanding Routers in Django-Rest-Framework

  • We can avoid configuring the URL’s with views.
  • Routers generate automatic URL patterns and maps every URL to its respective method based on a type of the request.
  • It deals with a little abstraction but, it can speed up the development process.
  • We can also speed up the debugging process as well with a little practice.
  • Router generates standardized url patterns.
  • We can expect consistent behaviour from viewsets and routers
  • We can avoid repetitive code in views. For example, in traditional views we need to have two api views for detail and list. But, we can achive it with a single ViewSet class.
  • If we develop a large api and if we don’t use viewset and routers then it will result in more views and urls. It will definitely affect our application(api) maintainablity and development time.
# serializers.py
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
# viewsets.py
from rest_framework import viewsets
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# urls.py
from django.conf.urls import url, include
from .viewsets import UserViewSet
router = routers.DefaultRouter()
router.register(r'custom_name', UserViewSet)
urlpatterns = [
# .......
url(r'^', include(router.urls, namespace="myapp")),
# .......
# Now, try the following in the django shell (python manage.py shell)
from test_rest.urls import router
for url in router.urls:
# You will get output like below
{'_regex_dict': {}, u'regex': <_sre.SRE_Pattern object at 0x7ff790dbd730>, 'name': u'user-list', 'callback': <function CUserViewSet at 0x7ff790dd1b18>, '_regex': u'^custom_name/$', 'default_args': {}}
{'_regex_dict': {}, 'callback': <function CUserViewSet at 0x7ff790dd1b18>, 'name': u'user-list', '_regex': u'^custom_name\\.(?P<format>[a-z0-9]+)/?$', 'default_args': {}}
{'_regex_dict': {}, u'regex': <_sre.SRE_Pattern object at 0x7ff790dbc280>, 'name': u'user-detail', 'callback': <function CUserViewSet at 0x7ff790dd1c08>, '_regex': u'^custom_name/(?P<pk>[^/.]+)/$', 'default_args': {}}
{'_regex_dict': {}, 'callback': <function CUserViewSet at 0x7ff790dd1c08>, 'name': u'user-detail', '_regex': u'^custom_name/(?P<pk>[^/.]+)\\.(?P<format>[a-z0-9]+)/?$', 'default_args': {}}
{'_regex_dict': {}, u'regex': <_sre.SRE_Pattern object at 0x7ff790e61ab0>, 'name': u'api-root', 'callback': <function APIRootView at 0x7ff790dd1cf8>, '_regex': u'^$', 'default_args': {}}
{'_regex_dict': {}, 'callback': <function APIRootView at 0x7ff790dd1cf8>, 'name': u'api-root', '_regex': u'^\\.(?P<format>[a-z0-9]+)/?$', 'default_args': {}}

The article was originally published at MicroPyramid blog




Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Tabletop Exercises for Engineering Teams

How To Get Started With Appium

How Ruby.Exchange Works: A 30,000-Foot View

Coursera’s Journey to GraphQL

Community Incentives Program (CIP) September Update!

Facebook Interview Question: Validate Binary Search Tree

How to find duplicates values using SQL

WISE-PaaS DCCS with python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company

More from Medium

Virtual environment set up for Django Application

Auto deploy Django projects on Heroku using GitHub repository

Django with Supervisor