Intro
In Django, particularly when working with Django REST Framework (DRF), there are several built-in router classes that help in automatically generating URL patterns for your API views. The main built-in routers provided by DRF include:
-
SimpleRouter
This is the most basic router, generating routes for list, create, retrieve, update, and destroy actions for your viewsets. It doesn't include any routes for actions like
extra actions
. -
DefaultRouter
This router extends
SimpleRouter
by additionally including a default root view that returns a list of all available API endpoints. This makes it very useful for creating a browsable API interface.
Which One is the Best?
-
DefaultRouter: If you want a straightforward setup with a browsable API interface,
DefaultRouter
is usually the best choice. It includes all the basic routes and a root API view, making it easy to navigate and test your API. -
SimpleRouter: If you prefer a more minimalistic setup without the root API view,
SimpleRouter
is a good choice. It’s lighter and doesn’t include the extra view thatDefaultRouter
provides. -
Custom Router: If you have very specific routing requirements that aren’t covered by
SimpleRouter
orDefaultRouter
, creating a custom router by subclassing one of these might be the best approach.
In most cases, DefaultRouter is the best option because it offers all the functionality of SimpleRouter plus the additional convenience of a root API view, making your API more user-friendly, especially during development.
Example
The SimpleRouter is the most basic router, generating routes for standard actions (list
, create
, retrieve
, update
, and destroy
) for your viewsets.
# views.py
from rest_framework import viewsets
from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
# urls.py
from rest_framework.routers import SimpleRouter
from myapp.views import MyModelViewSet
router = SimpleRouter()
router.register(r'mymodels', MyModelViewSet)
urlpatterns = router.urls
The DefaultRouter
extends SimpleRouter
by including a default root API view, which provides a browsable interface listing all the available endpoints.
# views.py
from rest_framework import viewsets
from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
# urls.py
from rest_framework.routers import DefaultRouter
from myapp.views import MyModelViewSet
router = DefaultRouter()
router.register(r'mymodels', MyModelViewSet)
urlpatterns = router.urls
These classes are used internally by SimpleRouter
and DefaultRouter
to define the patterns for the routes. They are typically not used directly but can be subclassed if you need a very customized router.
If you need something beyond what SimpleRouter
or DefaultRouter
offers, you can create a custom router by subclassing SimpleRouter or DefaultRouter.