Skip to content

Commit 660d240

Browse files
.action attribute on viewsets
1 parent 538d2e3 commit 660d240

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

rest_framework/viewsets.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def as_view(cls, actions=None, **initkwargs):
5959

6060
def view(request, *args, **kwargs):
6161
self = cls(**initkwargs)
62+
# We also store the mapping of request methods to actions,
63+
# so that we can later set the action attribute.
64+
# eg. `self.action = 'list'` on an incoming GET request.
65+
self.action_map = actions
6266

6367
# Bind methods to actions
6468
# This is the bit that's different to a standard view
@@ -87,6 +91,15 @@ def view(request, *args, **kwargs):
8791
view.suffix = initkwargs.get('suffix', None)
8892
return view
8993

94+
def initialize_request(self, request, *args, **kargs):
95+
"""
96+
Set the `.action` attribute on the view,
97+
depending on the request method.
98+
"""
99+
request = super(ViewSetMixin, self).initialize_request(request, *args, **kargs)
100+
self.action = self.action_map.get(request.method.lower())
101+
return request
102+
90103

91104
class ViewSet(ViewSetMixin, views.APIView):
92105
"""

0 commit comments

Comments
 (0)