Маршрут DRF ModelViewSet смешивается с маршрутом ApiView
У меня следующие взгляды:
class NotificationAPI(viewsets.ModelViewSet):
authentication_classes = (CustomJWTAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
queryset = Notification.objects.all()
def get_queryset(self):
qs = self.queryset.all()
queryset = qs.filter(recipient=self.request.user.id)
return queryset
class MarkAsReadView(APIView):
permission_classes = (IsAuthenticated,)
authentication_classes = [CustomJWTAuthentication, SessionAuthentication]
@swagger_auto_schema(manual_parameters=[authorization])
def post(self, request, notification_id):
# mark single notification as read
class MarkAllAsReadView(APIView):
permission_classes = (IsAuthenticated,)
authentication_classes = [CustomJWTAuthentication, SessionAuthentication]
@swagger_auto_schema(manual_parameters=[authorization])
def post(self, request):
#mark all notifications as read
и вот моя маршрутизация urls.py:
router = routers.DefaultRouter(trailing_slash=False)
router.register('notifications', NotificationAPI, basename='notifications')
urlpatterns = [
path('', include(router.urls)),
# notifications
path('notifications/mark-as-read/<int:notification_id>',
MarkAsReadView.as_view(), name='mark-as-read'),
path('notifications/mark-all-as-read',
MarkAllAsReadView.as_view(), name='mark-all-as-read'),
]
В настоящее время, когда я вызываю mark-as-read
, он работает, но когда я пытаюсь вызвать mark-all-as-read
api, он всегда возвращает следующую ошибку:
{
"detail": "Method \"POST\" not allowed."
}
Я проверил, и похоже, что маршрутизация уведомления modelviewset запутывает другие маршруты ApiView.
Я проверил, закомментировав ModelViewSet, и mark-all-as-read
теперь работает.
Почему это происходит? Разве маршрутизация не должна быть разделена на name
и basename
? Почему, когда я вызываю mark-all-as-read
, он переходит в маршрут ModelViewSet, а mark-as-read
работает нормально?