mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Add docs for collection routes
This commit is contained in:
parent
d72603bc6a
commit
5b11e23f6f
|
@ -92,7 +92,9 @@ The default routers included with REST framework will provide routes for a stand
|
||||||
def destroy(self, request, pk=None):
|
def destroy(self, request, pk=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
If you have ad-hoc methods that you need to be routed to, you can mark them as requiring routing using the `@link` or `@action` decorators. The `@link` decorator will route `GET` requests, and the `@action` decorator will route `POST` requests.
|
If you have ad-hoc methods that you need to be routed to, you can mark them as requiring routing using the `@collection_link`, `@collection_action`, `@link`, or `@action` decorators. The `@collection_link` and `@link` decorator will route `GET` requests, and the `@collection_action` and `@action` decorator will route `POST` requests.
|
||||||
|
|
||||||
|
The `@link` and `@action` decorators contain `pk` in their URL pattern and are intended for methods which require a single instance. The `@collection_link` and `@collection_action` decorators are intended for methods which operate on a collection of objects.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
|
@ -121,13 +123,20 @@ For example:
|
||||||
return Response(serializer.errors,
|
return Response(serializer.errors,
|
||||||
status=status.HTTP_400_BAD_REQUEST)
|
status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
The `@action` and `@link` decorators can additionally take extra arguments that will be set for the routed view only. For example...
|
@collection_link()
|
||||||
|
def recent_users(self, request):
|
||||||
|
recent_users = User.objects.all().order('-last_login')
|
||||||
|
page = self.paginate_queryset(recent_users)
|
||||||
|
serializer = self.get_pagination_serializer(page)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
The decorators can additionally take extra arguments that will be set for the routed view only. For example...
|
||||||
|
|
||||||
@action(permission_classes=[IsAdminOrIsSelf])
|
@action(permission_classes=[IsAdminOrIsSelf])
|
||||||
def set_password(self, request, pk=None):
|
def set_password(self, request, pk=None):
|
||||||
...
|
...
|
||||||
|
|
||||||
The `@action` decorator will route `POST` requests by default, but may also accept other HTTP methods, by using the `method` argument. For example:
|
The `@collection_action` and `@action` decorators will route `POST` requests by default, but may also accept other HTTP methods, by using the `method` argument. For example:
|
||||||
|
|
||||||
@action(methods=['POST', 'DELETE'])
|
@action(methods=['POST', 'DELETE'])
|
||||||
def unset_password(self, request, pk=None):
|
def unset_password(self, request, pk=None):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user