Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Bug related to theViewSet.action
attribute#9614
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
While working on a project, I came across an interesting bug that seems to have existed for a long time and fixing it is extremely straightforward. In the DRF documentation, there’s a section titled"Introspecting ViewSet actions". The code example provided there doesn't work. The issue arises in the As a result, if you try to use the AttributeError:'UserViewSet' object has no attribute'action' The definitialize_request(self,request,*args,**kwargs):""" Set the `.action` attribute on the view, depending on the request method. """method=request.method.lower()ifmethod=='options':# This is a special case as we always provide handling for the# options method in the base `View` class.# Unlike the other explicitly defined actions, 'metadata' is implicit.self.action='metadata'else:self.action=self.action_map.get(method)returnsuper().initialize_request(request,*args,**kwargs) |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 2 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I foundthis issue which asked something similar. I don't think of this as a bug, rather a limitation of the current implementation. DRF chose to initialize the request before doing anything else, and the methods you mention are used to built the request. The PS: as perthe contributing page, we try to minimise changes in DRF:
|
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
If we look at it from a conceptual perspective, you’re right. However in that case, we should update the section"Introspecting ViewSet actions" to avoid similar discussions in the future and save DRF users time. I suggest update this section like this: ##Introspecting ViewSet actionsDuring dispatch, the following attributes are available on the`ViewSet`.*`basename` - the base to use for the URL names that are created.*`action` - the name of the current action (e.g.,`list`,`create`).*`detail` - boolean indicating if the current action is configured for a list or detail view.*`suffix` - the display suffix for the viewset type - mirrors the`detail` attribute.*`name` - the display name for the viewset. This argument is mutually exclusive to`suffix`.*`description` - the display description for the individual view of a viewset.You may inspect these attributes to adjust behavior based on the current action.**Note**: don`t use the`action` attribute in the`get_parsers`,`get_authenticators` or`get_content_negotiator` methods, otherwise you will get an`AttributeError` error. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I created anissue, because I haven't received an answer for a long time. |
BetaWas this translation helpful?Give feedback.