rest_framework.routers

Routers provide a convenient and consistent way of automatically determining the URL conf for your API.

They are used by simply instantiating a Router class, and then registering all the required ViewSets with that router.

For example, you might have a urls.py that looks something like this:

router = routers.DefaultRouter() router.register(‘users’, UserViewSet, ‘user’) router.register(‘accounts’, AccountViewSet, ‘account’)

urlpatterns = router.urls

Module Contents

Route
DynamicRoute
class DynamicDetailRoute

Bases:object

classmethod __new__(cls, url, name, initkwargs)
class DynamicListRoute

Bases:object

classmethod __new__(cls, url, name, initkwargs)
escape_curly_brackets(url_path)

Double brackets in regex of url_path for escape string formatting

flatten(list_of_lists)

Takes an iterable of iterables, returns a single iterable containing all items

class RenameRouterMethods

Bases:django.utils.deprecation.RenameMethodsBase

renamed_methods = [None]
class BaseRouter

Bases:django.utils.six.with_metaclass()

urls
register(self, prefix, viewset, basename=None, base_name=None)
get_default_basename(self, viewset)

If basename is not specified, attempt to automatically determine it from the viewset.

get_urls(self)

Return a list of URL patterns, given the registered viewsets.

class SimpleRouter(trailing_slash=True)

Bases:rest_framework.routers.BaseRouter

routes
get_default_basename(self, viewset)

If basename is not specified, attempt to automatically determine it from the viewset.

get_routes(self, viewset)

Augment self.routes with any dynamically generated routes.

Returns a list of the Route namedtuple.

_get_dynamic_route(self, route, action)
get_method_map(self, viewset, method_map)

Given a viewset, and a mapping of http methods to actions, return a new mapping which only includes any mappings that are actually implemented by the viewset.

get_lookup_regex(self, viewset, lookup_prefix='')

Given a viewset, return the portion of URL regex that is used to match against a single instance.

Note that lookup_prefix is not used directly inside REST rest_framework itself, but is required in order to nicely support nested router implementations, such as drf-nested-routers.

https://github.com/alanjds/drf-nested-routers

get_urls(self)

Use the registered viewsets to generate a list of URL patterns.

class APIRootView

Bases:rest_framework.views.APIView

The default basic root view for DefaultRouter

_ignore_model_permissions = True
schema
api_root_dict
get(self, request, *args, **kwargs)
class DefaultRouter(*args, **kwargs)

Bases:rest_framework.routers.SimpleRouter

The default router extends the SimpleRouter, but also adds in a default API root view, and adds format suffix patterns to the URLs.

include_root_view = True
include_format_suffixes = True
root_view_name = api-root
default_schema_renderers
APIRootView
APISchemaView
SchemaGenerator
get_api_root_view(self, api_urls=None)

Return a basic root view.

get_urls(self)

Generate the list of URL patterns, including a default root view for the API, and appending .json style format suffixes.