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

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

Merged
carltongibson merged 6 commits intoencode:masterfromrpkilby:viewset-actions
Jul 6, 2018

Conversation

@rpkilby
Copy link
Contributor

@rpkilbyrpkilby commentedNov 17, 2017
edited
Loading

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:

change-password

The above changes include:

  • Breadcrumb and page title support custom names/titles for actions instead of just "User instance".
  • The method docstring is now used as the action's description.
  • A dropdown of "Extra Actions" (appropriately filtered to detail/non-detail actions).
  • Also, note that custom serializer classes were already supported.

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:

  • Note that these changes build off ofAdd '.basename' and '.reverse_action()' to ViewSet #5648 andRework dynamic list/detail actions #5705, which were spun out of this PR.
  • Views now acceptname anddescription via theinitkwargs.
    • This provides a general hook for customizing their display on the browsable API.
    • This is necessary to allow action methods to specify they're own name/description.
  • get_view_name andget_view_description now expect the View instance instead of a class.
  • get_view_name andget_view_description respect the new Viewname anddescription.
  • ViewSets considername andsuffix to be mutually exclusive.
  • Theaction decorator now allows users to specify thename, uses the docstring as the description.
  • AddsViewSet.get_extra_action_url_map for rendering extra actions.
  • Adds renderer/template code for both the browsable and admin APIs.
  • Adds method mapping to ViewSet actions. Multiple ViewSet methods can handle different HTTP methods of an action. ex:
    classMyViewSet(ViewSet):@action(detail=False)defexample(self,request,**kwargs):        ...@example.mapping.postdefcreate_example(self,request,**kwargs):        ...

TODO:

ryanbutterfield, tezkerek, kevinalh, soerface, jacobg, GProst, and h1lton reacted with thumbs up emojilovelydinosaur, tezkerek, noisy, Didericis, soerface, gabn88, and GProst reacted with heart emoji
@rpkilbyrpkilby added this to thev3.8 milestoneNov 17, 2017
@rpkilbyrpkilbyforce-pushed theviewset-actions branch 2 times, most recently from27a2e93 to7bac555CompareNovember 18, 2017 02:39
Copy link
Collaborator

@auvipyauvipy left a 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
Copy link
ContributorAuthor

I like the naming of action rater then detail/list route.

Agreed@auvipy. It's not so much that I'm in favor ofaction, more that I've never particularly likedlist_route/detail_route.

how many routable actions will be possible on a single viewset?

There isn't any limit, as there wasn't any limit to list/detail routes.

SIde note: more commits incoming

@auvipy
Copy link
Collaborator

It would be great to see some good example as part of this pr. thanks for the great work!

@rpkilby
Copy link
ContributorAuthor

@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
Copy link
Collaborator

How this change would affect this#5551?

@rpkilby
Copy link
ContributorAuthor

These changes aren't directly related to#5551.

warnings.warn(
"`detail_route` has been deprecated in favor of `action`, which accepts a "
"`detail` boolean. Use `@action(['GET'], detail=True)` instead.",
PendingDeprecationWarning
Copy link
Contributor

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.

rpkilby reacted with thumbs up emoji
Copy link
Collaborator

@carltongibsoncarltongibson left a 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. 👍

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 "
Copy link
Collaborator

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.

Copy link
ContributorAuthor

@rpkilbyrpkilbyNov 21, 2017
edited
Loading

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.

Copy link
Collaborator

@carltongibsoncarltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hey@rpkilby. This is pay off from#5705. Fancy re-doing from that as a base? (If it's not too much faff.)

@carltongibson
Copy link
Collaborator

Hey@rpkilby. Any appetite for pushing this over the line? It looks like it just needs docs and a few test cases

@carltongibsoncarltongibson removed this from the3.8 Release milestoneApr 3, 2018
@rpkilbyrpkilbyforce-pushed theviewset-actions branch 2 times, most recently from144c55d tob4d3eabCompareMay 16, 2018 09:26
@rpkilbyrpkilby changed the title[wip] Viewset extra actionsImprovements to ViewSet extra actionsMay 16, 2018
@rpkilbyrpkilby added this to the3.9 Release milestoneMay 16, 2018
@rpkilby
Copy link
ContributorAuthor

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 reacted with thumbs up emoji

Copy link
Collaborator

@carltongibsoncarltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hey@rpkilby.

Sorry for the slow review here.

Great stuff.

I'm expecting to see some docs here. 🙂

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`.
@rpkilbyrpkilbyforce-pushed theviewset-actions branch 4 times, most recently from597415d toc604380CompareJune 25, 2018 17:29
Ryan P Kilby added2 commitsJune 25, 2018 13:39
Removed old test logic around link/action decorators from `v2.3`. Alsosimplified the test by making the results explicit instead of computed.
@rpkilby
Copy link
ContributorAuthor

Okay, added some documentation. This includes:

  • Updated theVIEW_NAME_FUNCTION/VIEW_DESCRIPTION_FUNCTION docs to the new signature, and documented the relevantViewSet optional arguments (name,suffix,detail,description).
  • Document the new method mapping enhancement for theaction decorator.
auvipy reacted with thumbs up emoji

@carltongibsoncarltongibson merged commit0148a9f intoencode:masterJul 6, 2018
@rpkilbyrpkilby deleted the viewset-actions branchJuly 6, 2018 14:26
@noisy
Copy link

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
Copy link
ContributorAuthor

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
Copy link
Collaborator

@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.

pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull requestNov 17, 2020
* 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
Copy link
Contributor

gabn88 commentedMar 22, 2021
edited
Loading

@carltongibson@rpkilby
Great work with this addition!
I was wondering for almost a year why I couldn't get the "extra actions" button on my production site. In tests it worked, but on my live site I just couldn't get it working.
Until I saw:#7500 which fixed it right away.
Would it be possible to incorporate this?

feigner and drazvarin reacted with heart emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@auvipyauvipyauvipy left review comments

@carltongibsoncarltongibsoncarltongibson approved these changes

+1 more reviewer

@merwokmerwokmerwok left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.9.0 Release

Development

Successfully merging this pull request may close these issues.

Mark extra actions which corresponds to the same URL Support "extra actions" for routing in the HTML forms for the browsable API

7 participants

@rpkilby@auvipy@carltongibson@maryokhin@noisy@gabn88@merwok

[8]ページ先頭

©2009-2025 Movatter.jp