refactor: use correct Django paradigm for routes in url.py & views.py
banneux-florent opened this issue · comments
As I discovered, you can bind routes to ModelViewSet
classes using @action(detail=[bool], methods=[methods[]], url_path=[sub_path])
instead of defining routes separately. We should remove all the unnecessary separate routes to put them in their corresponding ModelViewSet
.
For example, when we close an item, at the moment, we call this route:
# urls.py
path("items/<int:item_id>/close", views.close_item, name='close_item'),
# views.py
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def close_item(request, item_id):
But we should instead add it to the ItemViewSet
as a new method, like that:
# views.py
@action(detail=True, methods=['post'], url_path=['close'])
def close(self, request, pk=None):