django_sorcery.routers module

Django REST Framework like router for viewsets.

class django_sorcery.routers.BaseRouter[source]

Bases: object

Base router.

get_default_base_name(viewset)[source]

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

get_urls()[source]

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

register(prefix, viewset, base_name=None)[source]

Registers a viewset for route generation.

urls

URL’s routed.

class django_sorcery.routers.DynamicRoute(url, name, detail, initkwargs)

Bases: tuple

detail

Alias for field number 2

initkwargs

Alias for field number 3

name

Alias for field number 1

url

Alias for field number 0

class django_sorcery.routers.Route(url, mapping, name, detail, initkwargs)

Bases: tuple

detail

Alias for field number 3

initkwargs

Alias for field number 4

mapping

Alias for field number 1

name

Alias for field number 2

url

Alias for field number 0

class django_sorcery.routers.SimpleRouter(trailing_slash=True)[source]

Bases: django_sorcery.routers.BaseRouter

Generates url patterns that map requests to a viewset’s action functions.

It will map the following operations to following actions on the viewset:

Method Path Action Route Name
GET /<resource>/ list <resource>-list
POST /<resource>/ create <resource>-list
GET /<resource>/new/ new <resource>-new
GET /<resource>/<pk>/ retrieve <resource>-detail
POST /<resource>/<pk>/ update <resource>-detail
PUT /<resource>/<pk>/ update <resource>-detail
PATCH /<resource>/<pk>/ update <resource>-detail
DELETE /<resource>/<pk>/ destroy <resource>-detail
GET /<resource>/<pk>/edit/ edit <resource>-edit
GET /<resource>/<pk>/delete/ confirm_destoy <resource>-delete
POST /<resource>/<pk>/delete/ destroy <resource>-delete
get_default_base_name(viewset)[source]

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

get_lookup_regex(viewset, lookup_prefix='')[source]

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_method_map(viewset, method_map)[source]

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_routes(viewset)[source]

Augment self.routes with any dynamically generated routes.

Returns a list of the Route namedtuple.

get_urls()[source]

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

routes = [Route(url='^{prefix}{trailing_slash}$', mapping={'get': 'list', 'post': 'create'}, name='{basename}-list', detail=False, initkwargs={'suffix': 'List'}), Route(url='^{prefix}/new{trailing_slash}$', mapping={'get': 'new'}, name='{basename}-new', detail=False, initkwargs={'suffix': 'New'}), DynamicRoute(url='^{prefix}/{url_path}{trailing_slash}$', name='{basename}-{url_name}', detail=False, initkwargs={}), Route(url='^{prefix}/{lookup}{trailing_slash}$', mapping={'get': 'retrieve', 'post': 'update', 'put': 'update', 'patch': 'update', 'delete': 'destroy'}, name='{basename}-detail', detail=True, initkwargs={'suffix': 'Instance'}), Route(url='^{prefix}/{lookup}/edit{trailing_slash}$', mapping={'get': 'edit'}, name='{basename}-edit', detail=True, initkwargs={'suffix': 'Instance'}), Route(url='^{prefix}/{lookup}/delete{trailing_slash}$', mapping={'get': 'confirm_destroy', 'post': 'destroy'}, name='{basename}-destroy', detail=True, initkwargs={'suffix': 'Instance'}), DynamicRoute(url='^{prefix}/{lookup}/{url_path}{trailing_slash}$', name='{basename}-{url_name}', detail=True, initkwargs={})]
django_sorcery.routers.action(methods=None, detail=None, url_path=None, url_name=None, **kwargs)[source]

Mark a ViewSet method as a routable action.

Set the detail boolean to determine if this action should apply to instance/detail requests or collection/list requests.

django_sorcery.routers.escape_curly_brackets(url_path)[source]

Double brackets in regex of url_path for escape string formatting.