Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Improvements to ViewSet extra actions#5605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
27a2e93 to7bac555Compare
auvipy left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I like the naming of action rater then detail/list route.
how many routable actions will be possible on a single viewset?
rpkilby commentedNov 18, 2017
Agreed@auvipy. It's not so much that I'm in favor of
There isn't any limit, as there wasn't any limit to list/detail routes. SIde note: more commits incoming |
auvipy commentedNov 18, 2017
It would be great to see some good example as part of this pr. thanks for the great work! |
rpkilby commentedNov 18, 2017
@auvipy, right now, there's a screenshot in the original comment, which should demonstrate the practical changes to the browsable API. As far as code goes, it's pretty much just: classMyViewSet(...):@action(methods=['get'],detail=True,name='Custom name'serializer_class=ThingySerializer)defdo_a_thing(self,request,pk,*args,**kwargs):""" # Docstrings are now used as descriptions! """ |
auvipy commentedNov 18, 2017
How this change would affect this#5551? |
rpkilby commentedNov 18, 2017
These changes aren't directly related to#5551. |
rest_framework/decorators.py Outdated
| warnings.warn( | ||
| "`detail_route` has been deprecated in favor of `action`, which accepts a " | ||
| "`detail` boolean. Use `@action(['GET'], detail=True)` instead.", | ||
| PendingDeprecationWarning |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think this needs astacklevel parameter so that the source code in the traceback points to the file using detail_route, not this file.
carltongibson left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
OK. Interesting.
Initial thought is 😱 breaking change but with the deprecation it's not a big issue I think.
I need to sit down with the implementation details but cool. 👍
rest_framework/decorators.py Outdated
| Used to mark a method on a ViewSet that should be routed for detail requests. | ||
| """ | ||
| warnings.warn( | ||
| "`detail_route` has been deprecated in favor of `action`, which accepts a " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is quite a big change for users. I think I'd like to see the message here saying, "pending deprecation" and "will be removed in 3.10" so that it's mega-clear what sort of timescale users have.
- This will require updating these messages for 3.9 (unless we're clever with the wording) but I think we need to be extra helpful here.
- I think this also merits a beginning on the release notes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Agreed. My main consideration right now is to determine if this is the path we ultimately want to move toward, and then work backwards from that w/ deprecations and whatnot.
Right now, the state of the PR is more exploratory. For example, I also want to try and fix#4920, which will require more modifications to the dynamic route generation.
carltongibson left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
carltongibson commentedMar 26, 2018
Hey@rpkilby. Any appetite for pushing this over the line? It looks like it just needs docs and a few test cases |
144c55d tob4d3eabComparerpkilby commentedMay 16, 2018
Hi all - the PR is largely done and ready for review (documentation not withstanding). In addition to tests, the updates implements#4920 and copies the "Extra Actions" dropdown from the Browsable API to the Admin API template. |
carltongibson left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ViewSets may now provide their `name` and `description` attributesdirectly, instead of relying on view introspection to derive them.These attributes may also be provided with the view's initkwargs.The ViewSet `name` and `suffix` initkwargs are mutually exclusive.The `action` decorator now provides the `name` and `description` tothe view's initkwargs. By default, these values are derived from themethod name and its docstring. The `name` may be overridden by providingit as an argument to the decorator.The `get_view_name` and `get_view_description` hooks now provide theview instance to the handler, instead of the view class. The defaultimplementations of these handlers now respect the `name`/`description`.
597415d toc604380CompareRemoved old test logic around link/action decorators from `v2.3`. Alsosimplified the test by making the results explicit instead of computed.
rpkilby commentedJun 25, 2018
Okay, added some documentation. This includes:
|
noisy commentedJul 14, 2018
I just found this pull request, because I tried to make my actions visible in browsable api. Thank you for this work. Right now I am developing my project installing DRF directly from git.@carltongibson - do you know when we can expect a new version of DRF with this feature included? |
rpkilby commentedJul 14, 2018
Brief aside: I know there is a little more work that needs to be done before a 3.9 release can be shipped, but one minor blocker is#6081, which fixes a flaw in this pull request. Ishould have time to work on it this weekend. |
carltongibson commentedJul 15, 2018
@noisy we’re working on the 3.9 release now. I would think a few weeks. Pip’s VCS support is first class. There’s no reason why you can’t use the version from master. |
* View suffix already set by initializer* Add 'name' and 'description' attributes to ViewSetViewSets may now provide their `name` and `description` attributesdirectly, instead of relying on view introspection to derive them.These attributes may also be provided with the view's initkwargs.The ViewSet `name` and `suffix` initkwargs are mutually exclusive.The `action` decorator now provides the `name` and `description` tothe view's initkwargs. By default, these values are derived from themethod name and its docstring. The `name` may be overridden by providingit as an argument to the decorator.The `get_view_name` and `get_view_description` hooks now provide theview instance to the handler, instead of the view class. The defaultimplementations of these handlers now respect the `name`/`description`.* Add 'extra actions' to ViewSet & browsable APIs* Update simple router testsRemoved old test logic around link/action decorators from `v2.3`. Alsosimplified the test by making the results explicit instead of computed.* Add method mapping to ViewSet actions* Document extra action method mapping
gabn88 commentedMar 22, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@carltongibson@rpkilby |
Uh oh!
There was an error while loading.Please reload this page.
Resolves#2062,#2934,#3271,#5755
Fixes#4920
This will obviously require more extensive testing and documentation updates, but I'd like to hammer out the implementation details first. But first, here's a preview of the changes to the browsable API:
The above changes include:
To implement this, it was necessary to change how routers and viewsets interact. At a higher level:
Views more explicitly customize their display name and description.ViewSets are provided more context/configuration from the router. For example, it can now determine if the current request is operation on a list or detail action.In more detail detail:
Views now acceptnameanddescriptionvia theinitkwargs.get_view_nameandget_view_descriptionnow expect the View instance instead of a class.get_view_nameandget_view_descriptionrespect the new Viewnameanddescription.ViewSets considernameandsuffixto be mutually exclusive.actiondecorator now allows users to specify thename, uses the docstring as the description.ViewSet.get_extra_action_url_mapfor rendering extra actions.TODO:
AutoSchema fails when set on detail_route #5630Rename/deprecatebase_nametobasenamefor consistency.