Understanding Routers in Django-Rest-Framework

We can use function-based views(FBV) and generic views(class-based views[CBV]) to develop rest API by using the Django-REST-Framework. It’s a good approach to use FBV or CBV with defined URL configurations. Mapping views with the URL’s is a good idea but, probably not the best. We can better organize this by using Routers and ViewSets.

Advantages using ViewSets and Routers over traditional views

  • 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.

Lets see how routers generate dynamic urls

After writing the viewsets we have register the viewsets with “DefaultRouter”. You can look into urls.py in above code to know how to register a viewset. We have discussed that router generates urls automatically. In above code we have used djnago shell to know how routers are generating the urls automatically. From above code, we can find that every url has keys “_regex”, “name”, “callback”.

_regex: It conains the reggular expression of url. we gave “custom_name” prefix while registering the ViewSet. By using prefix router formed regular expressions u’^custom_name/$’, u’^custom_name/(?P<pk>[^/.]+)/$’. The pattern which contains “pk” as keyword argument will matches methods “detail”, “update”, “partial_update”, “delete” based on type of the request. The pattern which doesn’t have “pk” matches the method “list”.

name: It is used to renerate the url by using “reverse”. It will form names “{basename}-list”, “{basename}-detail” based on model name. In above code, I have used a model with name “User”. That’s the reason router generated the names “user-list”, “user-detail”. By default basename generally taken as lowercase of model name. Otherwise we have to pass it as a third parameter like “router.register(prefix=r’custom_name’, viewset=UserViewSet, base_name=”custom_name”)”

callback: It is a method of the ViewSet. It is mapped to the url based on type of the request.

As we discussed earlier ViewSets reduces writing of more views. That’s the reason router generated the more urls for ViewSet. It will generate the urls based on the methods that the ViewSet contains.





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

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