Home > Net >  Why my drf-swagger drf-yasg api documentation is not categorized according to models?
Why my drf-swagger drf-yasg api documentation is not categorized according to models?

Time:12-29

although I have used same setting for both projects for documentation. Their documentation view is different. I want view like first one, Where api is categorized according to models.

urls.py - for both

from drf_yasg import openapi
from drf_yasg.views import get_schema_view

schema_view = get_schema_view(
    openapi.Info(
        title="Nepal Hearing & Speech Care Center - Nepal",
        default_version="v1",
        description="API Documentation",
        terms_of_service="2022",
        contact=openapi.Contact(email="[email protected]"),
        license=openapi.License(name="Private Project"),
    ),
    public=False,
)


urlpatterns = (
    [
        path(
            "doc",
            schema_view.with_ui("swagger", cache_timeout=0),
            name="schema-ui",
        ),
]

You can check photos for both Projects:

Project 1

Project 1 (Required)

Project 2

Project 2 (to be changed)

CodePudding user response:

You can tags to categorize your API endpoints in drf swagger (drf-yasg), you can use the '@swagger_auto_schema' decorator and specify the 'tags' parameter.

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

Or you can also specify tags for your models by using the tags field in the Meta class of your serializer. 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.

CodePudding user response:

@swagger_auto_schema(tags=['my_apis'])
class DepartmentViewSet(viewsets.ModelViewSet):
    
    permission_classes = [IsAuthenticated]
    queryset = Department.objects.all()
    #filter_class = 
    #filter_backends = (SearchFilter, OrderingFilter, DjangoFilterBackend)
    search_fields = []
    ordering_fields = []
    ordering = []
    http_method_names = ["options", "head", "post", "get", "patch"]
    
    def get_serializer_class(self):
        serializer_class = DepartmentListSerializer
        if self.request.method == "GET":
            if self.action == "list":
                serializer_class = DepartmentListSerializer
            else: 
                serializer_class = DepartmentRetrieveSerializer
        if self.request.method == "POST":
            serializer_class = DepartmentCreateSerializer
        elif self.request.method == "PATCH":
            serializer_class = DepartmentPatchSerializer
        
        return serializer_class

I changed my view to this. But still same issue.

  • Related