简体   繁体   English

有没有办法为我的 drf-yasg 端点提供自定义名称?

[英]is there a way to provide custom names for my drf-yasg endpoints?

i am implementing an API using django-rest-framework and using drf-yasg to document and name my endpoints every post method ends with create我正在使用 django-rest-framework 实现一个 API,并使用 drf-yasg 来记录和命名我的端点,每个 post 方法都以 create 结尾

i tried searching through the documentation and i cant find a way to do it我尝试搜索文档,但找不到方法

You can use tags to categorize your API endpoints in drf swagger (drf-yasg), you can use the '@swagger_auto_schema' decorator and specify the 'tags' parameter.您可以使用标签在 drf swagger (drf-yasg) 中对您的 API 端点进行分类,您可以使用“@swagger_auto_schema”装饰器并指定“标签”参数。

For example, if you have a view that lists all users in your system, you can use the @swagger_auto_schema decorator to specify the users tag like this:例如,如果您有一个列出系统中所有用户的视图,您可以使用 @swagger_auto_schema 装饰器来指定用户标签,如下所示:

@swagger_auto_schema(tags=['users'])
def list_users(request):
    # code to list users
    pass

This will cause the API endpoint for the list_users view to be displayed under the users tag in the generated documentation.这将导致 list_users 视图的 API 端点显示在生成的文档中的用户标签下。

You can also specify tags for your models by using the tags field in the Meta class of your serializer.您还可以使用序列化程序的 Meta class 中的标签字段为您的模型指定标签。 For example:例如:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email']
        tags = ['users']

This will cause the UserSerializer to be displayed under the users tag in the generated documentation.这将导致 UserSerializer 显示在生成的文档中的用户标签下。

As someone mentioned in the comment, you should use the @swagger_auto_schema() decorator for your API in order to customize it.正如评论中提到的那样,您应该为 API 使用@swagger_auto_schema()装饰器以对其进行自定义。 then set operation_id to change its name in swagger docs.然后设置operation_id以在 swagger 文档中更改其名称。

here is an exact example that'll solve your problem: drf-yasg: How to change operationId?这是一个可以解决您的问题的确切示例: drf-yasg: How to change operationId?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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