Skip to content
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

How to document ModelViewSet methods? #182

Closed
tonioo opened this issue Oct 27, 2020 · 12 comments
Closed

How to document ModelViewSet methods? #182

tonioo opened this issue Oct 27, 2020 · 12 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@tonioo
Copy link

tonioo commented Oct 27, 2020

Hi,
is there a way to document ModelViewSet default methods (list, retrieve, etc.) without overloading them? I've tried to put some documentation in the class level docstring but it's not working:

"""
viewset.

retrieve: Return the given instance.

list: Return a list of all existing instances.

create: Create a new instance.
"""
@tfranzel
Copy link
Owner

Hi @tonioo, we don't do interpretation of the docstring. what we do support is having a docstring on the view class that gets propagated to each endpoint in that view. @extend_schema may then override what was found in the class docstring.

class level docstring but it's not working

by that you mean it is not split up? the whole docstring should show up in description though.

i'm but hesitant about introducing docstring processing.

@tonioo
Copy link
Author

tonioo commented Oct 27, 2020

@tfranzel yes, I mean it is not split up but it's the expected behavior according to what you say.

I can indeed use the @extend_schema decorator but it means I need to overload every default method just for the documentation... Maybe a class decorator would be a better answer?

@tfranzel
Copy link
Owner

yes, that is the expected behaviour currently.

@extend_schema on the view is a good idea in theory. actually it already works on views with limited functionality. however, i made a design mistake there, which is hard to recover from. the problem is that the class decorator is applied after the method decorator. thus mixing both could lead to hard to understand override mechanics, but i have not given up on this yet.

in comparison docstring parsing would probably be the solution with the least amount of surprises. its not an ideal situation.

@tfranzel
Copy link
Owner

i figured out a way to resolve the above mentioned "override mechanics" problem. after we fix that, the path is open to add this functionality via view decoration.

@tonioo
Copy link
Author

tonioo commented Oct 29, 2020

@tfranzel Good news. What's your idea?

@tfranzel
Copy link
Owner

something in the direction of

@extend_schem_view(
    retrieve=extend_schema(request=Foo, response=Foo)
    custom_action=extend_schema(request=Foo, response=Foo)
)
class YourModelViewset(viewsets.ModelViewSet)
    queryset = M.objects.none()

@tonioo
Copy link
Author

tonioo commented Oct 29, 2020

@tfranzel Makes sense.

@tfranzel tfranzel added the enhancement New feature or request label Oct 31, 2020
@v1k45
Copy link
Contributor

v1k45 commented Oct 31, 2020

@tonioo You can also use method_decorator to achieve this:

@method_decorator(extend_schema(operation_id='patchPost'), name='partial_update')
@method_decorator(extend_schema(operation_id='deletePost'), name='destroy')
class PostViewSet(viewsets.ModelViewSet):
    serializer_class = PostSerializer
    queryset = Post.objects.all()

@tfranzel This however, doesn't seem to work with custom actions. Custom actions have to be decorated explicitly while defining.

@tfranzel
Copy link
Owner

tfranzel commented Oct 31, 2020

~~@v1k45 a good idea in principle but i recommend not doing that. @extend_schema is an invasive (non-pure) decorator. by using @method_decorator, you are most likely patching def partial_update(): in the UpdateModelMixin and thus having your extend_schema changes reflected in all views using the UpdateModelMixin.

currently working on extend_schema_view which takes that issue into account. it's close to done.

Edit: it turns out it's not as bad as i thought. DRF and the method_decorator are doing something smarter, but i have not completetly understood how. a quick test did not produce the problem is was expecting. anyway, i would be cautious.

@tfranzel
Copy link
Owner

tfranzel commented Nov 1, 2020

@v1k45 as it turns out, method_decorator is doing something quite smart and is safe to use, but as you noticed this smart behavior does not always work for us.

@tonioo i added the new extend_schema_view decorator as outlined. please thoroughly check if everything works out fine. it was a tricky change.

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Nov 1, 2020
@v1k45
Copy link
Contributor

v1k45 commented Nov 1, 2020

extend_schema_view is certainly a better and cleaner solution for this. Thanks!

@hmpf
Copy link

hmpf commented Nov 9, 2020

Works for me as well.

@tfranzel tfranzel closed this as completed Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

4 participants