https://www.django-rest-framework.org/api-guide/routers/
Usage
Here's an example of a simple URL conf, that uses SimpleRouter.
from rest_framework import routers
router = routers.SimpleRouter()
router.register(r'users', UserViewSet)
router.register(r'accounts', AccountViewSet)
urlpatterns = router.urlsThere are two mandatory arguments to the register() method:
prefix- The URL prefix to use for this set of routes.viewset- The viewset class.
Optionally, you may also specify an additional argument:
basename- The base to use for the URL names that are created. If unset the basename will be automatically generated based on thequerysetattribute of the viewset, if it has one. Note that if the viewset does not include aquerysetattribute then you must setbasenamewhen registering the viewset.
The example above would generate the following URL patterns:
- URL pattern:
^users/$Name:'user-list' - URL pattern:
^users/{pk}/$Name:'user-detail' - URL pattern:
^accounts/$Name:'account-list' - URL pattern:
^accounts/{pk}/$Name:'account-detail'
DefaultRouter
This router is similar to SimpleRouter as above, but additionally includes a default API root view, that returns a response containing hyperlinks to all the list views. It also generates routes for optional .json style format suffixes.
| URL Style | HTTP Method | Action | URL Name |
|---|---|---|---|
| [.format] | GET | automatically generated root view | api-root |
| {prefix}/[.format] | GET | list | {basename}-list |
| POST | create | ||
| {prefix}/{url_path}/[.format] | GET, or as specified by `methods` argument | `@action(detail=False)` decorated method | {basename}-{url_name} |
| {prefix}/{lookup}/[.format] | GET | retrieve | {basename}-detail |
| PUT | update | ||
| PATCH | partial_update | ||
| DELETE | destroy | ||
| {prefix}/{lookup}/{url_path}/[.format] | GET, or as specified by `methods` argument | `@action(detail=True)` decorated method | {basename}-{url_name} |
As with SimpleRouter the trailing slashes on the URL routes can be removed by setting the trailing_slash argument to False when instantiating the router.
router = DefaultRouter(trailing_slash=False)https://www.django-rest-framework.org/api-guide/routers/#defaultrouter
No comments:
Post a Comment