-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
27a2e93 to
7bac555
Compare
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?
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 |
6ef27c6 to
eafe0cc
Compare
|
It would be great to see some good example as part of this pr. thanks for the great work! |
|
@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: class MyViewSet(...):
@action(methods=['get'], detail=True, name='Custom name'
serializer_class=ThingySerializer)
def do_a_thing(self, request, pk, *args, **kwargs):
"""
# Docstrings are now used as descriptions!
""" |
|
How this change would affect this #5551? |
|
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 a stacklevel 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.
a8dff35 to
c028686
Compare
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.
c028686 to
5207a3f
Compare
|
Hey @rpkilby. Any appetite for pushing this over the line? It looks like it just needs docs and a few test cases |
144c55d to
b4d3eab
Compare
|
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` attributes directly, 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` to the view's initkwargs. By default, these values are derived from the method name and its docstring. The `name` may be overridden by providing it as an argument to the decorator. The `get_view_name` and `get_view_description` hooks now provide the view instance to the handler, instead of the view class. The default implementations of these handlers now respect the `name`/`description`.
597415d to
c604380
Compare
Removed old test logic around link/action decorators from `v2.3`. Also simplified the test by making the results explicit instead of computed.
|
Okay, added some documentation. This includes:
|
|
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? |
|
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. I should have time to work on it this weekend. |
|
@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 ViewSet ViewSets may now provide their `name` and `description` attributes directly, 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` to the view's initkwargs. By default, these values are derived from the method name and its docstring. The `name` may be overridden by providing it as an argument to the decorator. The `get_view_name` and `get_view_description` hooks now provide the view instance to the handler, instead of the view class. The default implementations of these handlers now respect the `name`/`description`. * Add 'extra actions' to ViewSet & browsable APIs * Update simple router tests Removed old test logic around link/action decorators from `v2.3`. Also simplified the test by making the results explicit instead of computed. * Add method mapping to ViewSet actions * Document extra action method mapping
|
@carltongibson @rpkilby |
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.