Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" 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 """
""" Partially format a format_string, swapping out any '{methodname}' or '{methodnamehyphen}' components. """
""" If `base_name` is not specified, attempt to automatically determine it from the viewset. """ raise NotImplemented('get_default_base_name must be overridden')
""" Return a list of URL patterns, given the registered viewsets. """ raise NotImplemented('get_urls must be overridden')
def urls(self):
# List route. Route( url=r'^{prefix}{trailing_slash}$', mapping={ 'get': 'list', 'post': 'create' }, name='{basename}-list', initkwargs={'suffix': 'List'} ), # Detail route. Route( url=r'^{prefix}/{lookup}{trailing_slash}$', mapping={ 'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy' }, name='{basename}-detail', initkwargs={'suffix': 'Instance'} ), # Dynamically generated routes. # Generated using @action or @link decorators on methods of the viewset. Route( url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', mapping={ '{httpmethod}': '{methodname}', }, name='{basename}-{methodnamehyphen}', initkwargs={} ), ]
""" If `base_name` is not specified, attempt to automatically determine it from the viewset. """
'not automatically determine the name from the viewset, as ' \ 'it does not have a `.model` or `.queryset` attribute.'
""" Augment `self.routes` with any dynamically generated routes.
Returns a list of the Route namedtuple. """
# Determine any `@action` or `@link` decorated methods on the viewset
# Dynamic routes (@link or @action decorator) url=replace_methodname(route.url, methodname), mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), name=replace_methodname(route.name, methodname), initkwargs=initkwargs, )) else: # Standard route
""" 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. """
""" Given a viewset, return the portion of URL regex that is used to match against a single instance. """
""" Use the registered viewsets to generate a list of URL patterns. """
# Only actions which actually exist on the viewset will be bound continue
# Build the url pattern prefix=prefix, lookup=lookup, trailing_slash=self.trailing_slash )
""" The default router extends the SimpleRouter, but also adds in a default API root view, and adds format suffix patterns to the URLs. """
""" Return a view to use as the API root. """
ret = {} for key, url_name in api_root_dict.items(): ret[key] = reverse(url_name, request=request, format=format) return Response(ret)
""" Generate the list of URL patterns, including a default root view for the API, and appending `.json` style format suffixes. """
|