Routing
API Reference: Routing¶
The routing layer exposes a single public class, SanicRouter: it splits a ViewSet into collection routes, detail routes, and @action routes, then registers them on a Sanic application or Blueprint. See Routing for usage.
from srf.route import SanicRouter
router = SanicRouter(app, prefix="/api/v1")
router.register("books", BookViewSet)
SanicRouter ¶
DRF-style router that registers a ViewSet as collection/detail routes on a
Sanic app or Blueprint, and auto-discovers @action methods.
Route names follow {name}-list, {name}-detail and {name}-{url_name}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Sanic | Blueprint
|
Sanic app or Blueprint to register routes on. |
required |
prefix
|
str
|
Optional URL prefix, e.g. |
''
|
register ¶
Register view_cls under path.
Only implemented actions get routes: GET/POST {path} map to
list/create, GET/PUT/PATCH/DELETE {path}/<pk> map to
retrieve/update/update/destroy. Methods decorated with
@action are discovered and registered automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Resource path, e.g. |
required |
view_cls
|
type[GenericAPIView]
|
The ViewSet class to register. |
required |
name
|
str | None
|
Route name prefix; defaults to |
None
|
options: heading_level: 3 members: - init - register show_source: false inherited_members: false
Registration result¶
register(path, view_cls, name=None) only registers routes for view methods that actually exist on the class:
| URL | HTTP | View method | Route name |
|---|---|---|---|
{prefix}/{path} |
GET |
list |
{name}-list |
{prefix}/{path} |
HEAD |
list |
{name}-list |
{prefix}/{path} |
POST |
create |
{name}-list |
{prefix}/{path}/<pk> |
GET |
retrieve |
{name}-detail |
{prefix}/{path}/<pk> |
HEAD |
retrieve |
{name}-detail |
{prefix}/{path}/<pk> |
PUT |
update |
{name}-detail |
{prefix}/{path}/<pk> |
PATCH |
update |
{name}-detail |
{prefix}/{path}/<pk> |
DELETE |
destroy |
{name}-detail |
{prefix}/{path}/{url_path} |
action.methods |
Method decorated with action |
{name}-{url_name} |
{prefix}/{path}/<pk>/{url_path} |
action.methods |
Action with detail=True |
{name}-{url_name} |
namedefaults topath.- If a ViewSet does not define
list, no collection route is created; the same applies to detail routes. GETroutes also supportHEAD, reusing the samelist/retrievemethod.- A read-only ViewSet generates only the routes for
list/retrieve(includingHEAD).
Discovery rules for @action¶
register() uses inspect.getmembers() to scan the view class (including inherited methods); any method with an extra_info attribute is registered as a separate route. extra_info is written by action:
| Parameter | Default | Purpose |
|---|---|---|
detail |
False |
When True, the URL includes <pk> |
methods |
("GET",) |
HTTP methods accepted by the action |
url_path |
"/{function name}" |
Appended after the collection/detail path |
url_name |
Function name | Used to build the route name {name}-{url_name} |
Prefix normalization
prefix="api/v1/" is normalized to /api/v1, and leading/trailing / in path are removed. Therefore SanicRouter(app, prefix="api/v1").register("/books/", BookViewSet) produces the collection URL /api/v1/books.
Duplicate route names
SanicRouter does not check for route name conflicts. If two ViewSets use the same name, or two actions use the same url_name, you must ensure uniqueness yourself; otherwise Sanic reports a duplicate route name.