I have the following views:
@extend_schema(tags=["admin"])
class CompanyForAdminView(APIView):
permission_classes = [IsAuthenticated, IsAdmin]
filter_backends = [SearchFilter]
search_fields = ["email", "company_profile__name"]
def get(self, request, id=None):
if id:
company = get_object_or_404(Company, id=id)
serializer = CompanyRetrieveSerializer(company)
return Response(serializer.data)
else:
companies = Company.objects.all()
for backend in list(self.filter_backends):
companies = backend().filter_queryset(request, companies, self)
paginator = LimitOffsetPagination()
result = paginator.paginate_queryset(companies, request)
serializer = CompanyListSerializer(result, many=True)
return paginator.get_paginated_response(serializer.data)
@extend_schema(tags=["admin"])
class EmployeeForAdminView(APIView):
permission_classes = [IsAuthenticated, IsAdmin]
filter_backends = [SearchFilter]
search_fields = ["first_name", "middle_name", "last_name", "designation"]
def get(self, request, company_id, id=None):
if id:
employee = get_object_or_404(Employee, id=id)
serializer = EmployeeRetrieveSerializer(employee)
return Response(serializer.data)
else:
company = get_object_or_404(Company, id=company_id)
employees = company.employees.all()
for backend in list(self.filter_backends):
employees = backend().filter_queryset(request, employees, self)
paginator = LimitOffsetPagination()
result = paginator.paginate_queryset(employees, request)
serializer = EmployeeListSerializer(result, many=True)
return paginator.get_paginated_response(serializer.data)
I have two different routes for each view in my urls.py
.
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from drf_spectacular.utils import extend_schema_view, extend_schema
from account.views import (
CompanyView,
EmployeeView,
AddEmployeesFromExcelView,
PasswordChangeView,
ForgotPasswordAPIView,
ResetPasswordTokenCheckAPIView,
CompanyForAdminView,
EmployeeForAdminView,
)
app_name = "account"
...
urlpatterns = [
...,
path("admin/company/", CompanyForAdminView.as_view(), name="admin_companies"),
path(
"admin/company/<int:id>/", CompanyForAdminView.as_view(), name="admin_companies"
),
path(
"admin/<int:company_id>/employee/",
extend_schema_view(
get=extend_schema(
summary="List Employees",
description="List of employees for a given company.",
tags=["admin"],
)
)(EmployeeForAdminView.as_view()),
name="admin_employees",
),
path(
"admin/<int:company_id>/employee/<int:id>/",
extend_schema_view(
get=extend_schema(
summary="Retrieve Employee",
description="Retrieve details of a specific employee.",
tags=["admin"],
)
)(EmployeeForAdminView.as_view()),
name="admin_employees",
),
]
I am trying to add different description and docs for SwaggerUI for the different endpoints. But when I do as I have shown above, both the endpoint end up having the same description which is the description of the lower endpoint (admin/<int:company_id>/employee/<int:id>/
).
How can I write url endpoint level descriptions using drf-spectacular?