Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bug related to theViewSet.action attribute#9614

Unanswered
EXG1O asked this question inPotential Issue
Discussion options

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 theViewSetMixin.initialize_request method. Here, we call the parent methodinitialize_request, which in turn invokes the methodsget_parsers,get_authenticators andget_content_negotiator to create aRequest instance. However, only after creating theRequest instance is theaction attribute assigned to the class.

As a result, if you try to use theaction attribute in theget_parsers,get_authenticators orget_content_negotiator methods, you will encounter an error:

AttributeError:'UserViewSet' object has no attribute'action'

TheViewSetMixin.initialize_request method can be fixed like this:

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)
You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

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.

Themethod attribute doesn't exists on the DRFRequest class, so I assume it's proxied to the DjangoHttpRequest. In theory your suggestionshould work, but conceptually, it feels wrong to lookup the action before we've initialized the DRFRequest.

PS: as perthe contributing page, we try to minimise changes in DRF:

At this point in its lifespan we consider Django REST framework to be feature-complete. We focus on pull requests that track the continued development of Django versions, and generally do not accept new features or code formatting changes.

You must be logged in to vote
2 replies
@EXG1O
Comment options

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.
@EXG1O
Comment options

I created anissue, because I haven't received an answer for a long time.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@EXG1O@browniebroke

[8]ページ先頭

©2009-2025 Movatter.jp