ModelViewSet with additional methods

54 views
Skip to first unread message

Stats Student

unread,
Mar 7, 2021, 5:41:15 PM3/7/21
to django-res...@googlegroups.com
Hi, I have a ModelViewSet implementation which is routed through the
DefaultRouter and everything works fine.

router = routers.DefaultRouter()
router.register(r'item', views.ItemViewSet, 'item')

class ItemViewSet(viewsets.ModelViewSet):
serializer_class = ItemSerializer

I would now like to add more methods to this setup by which the caller
can submit a POST request with a list of items to update, e.g. [ {
"id": 1, "prop": 1 } , { "id": 2, "prop": 4} ]
And I would like this method to be accessible through the [
/api/item/update_prop/ ] endpoint.

Currently, I am doing this by declaring a new class which inherits
from APIView and manually add a URL mapping to [
/api/item_update_prop/ ] which I do not particularly like.

I tried doing it through @action(detail=False, methods=['post']) but
kept getting an error that the method expects a dictionary that the
serializer needs to parse, but is receiving a list instead.

Thanks for any insight on how to best do this.
Reply all
Reply to author
Forward
0 new messages