简体   繁体   中英

Have a specific docstring for each methods in @action, Django-REST-framework

In Django-REST-framework, in a ViewSet , I use extra routing actions using the @action decorator on several methods. I would like to have different docstrings for each methods, but obviously this doesn't work. Any idea?

@action(methods=['GET', 'POST', 'DELETE'])
def followers(self, request, player):

    if request.method == 'GET':
        """
        Get the players who follow this player
        """
        [...]

    elif request.method == 'POST':
        """
        Follow this player
        """
        [...]

    [...]

Generated doc with Swagger: http://i.stack.imgur.com/8kzUw.png

Any method may have only one docstring, so generally you have two options:

  1. You can define a single docstring to the followers method and explain how method behaves accordingly to the method. This option is suitable if you are sure that the business logic of the different HTTP methods is not too complex.

  2. Define separate methods for different request types, so that they could have separate docstrings. This will allow you to keep the logic inside each method as simple as possible.

You can also consider returning an object that has separate methods representing the HTTP methods and calling them accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM