Skip to content

Commit 963607a

Browse files
committed
Improve documentation regarding serializer class on extra actions.
The documentation does currently not describe how to use a custom `serializer_class` with extra actions. The example uses a custom serializer without using the seralizer_class attribute, resulting in the generated documentation page presenting the wrong form. The author of the feature described the usage of the `serializer_class` kwarg in his pr #5605: #5605 (comment)
1 parent 271c4c5 commit 963607a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

docs/api-guide/viewsets.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ You may inspect these attributes to adjust behaviour based on the current action
129129

130130
If you have ad-hoc methods that should be routable, you can mark them as such with the `@action` decorator. Like regular actions, extra actions may be intended for either a single object, or an entire collection. To indicate this, set the `detail` argument to `True` or `False`. The router will configure its URL patterns accordingly. e.g., the `DefaultRouter` will configure detail actions to contain `pk` in their URL patterns.
131131

132+
Your custom action may use a different [Serializer class](serializers.md). To make sure that the generated docs display a form which matches your serializer, you have two options:
133+
134+
* Either: provide a `serializer_class=MySerializer` argument to the `@action()` decorator (see example below)
135+
* Or: override the `get_serializer_class()` method as described in [GenericAPIView](generic-views.md#genericapiview)
136+
132137
A more complete example of extra actions:
133138

134139
from django.contrib.auth.models import User
@@ -144,7 +149,7 @@ A more complete example of extra actions:
144149
queryset = User.objects.all()
145150
serializer_class = UserSerializer
146151

147-
@action(detail=True, methods=['post'])
152+
@action(detail=True, methods=['post'], serializer_class=PasswordSerializer)
148153
def set_password(self, request, pk=None):
149154
user = self.get_object()
150155
serializer = PasswordSerializer(data=request.data)

0 commit comments

Comments
 (0)