Supporting additional methods for a viewset's list route #7851
-
I have a normal viewset that supports the standard actions
I would like to add an additional action on the viewset: So it seems like you should just be able to define a new action on the viewset. I don't want to add any elements to the route, I only want to add an additional method to the @action(detail=False, methods=["PATCH"], url_path="")
def bulk_update(self, request, *args, **kwargs):
... Unfortunately this is not supported because the ...
func.url_path = url_path if url_path else func.__name__
... Is there a good way to do this using an action on the ViewSet? I am really struggling to even find a work around for this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @rvinzent, did you manage to solve this? I'm looking to achieve the same now. |
Beta Was this translation helpful? Give feedback.
-
@adamckay we ended up going with a bit of a work around. Our use case is a bulk update endpoint at something like We have the normal viewset class like class SomeResourceViewSet(viewsets.ViewSet):
...
# note: do NOT add the @action decorator
def bulk_update(self, request): ... Apparently the fixed_resource_list_view = SomeResourceViewSet.as_view(
{
"get": "list",
"post": "create",
"patch": "bulk_update",
}
) Then assign this view it as the first item in router = SimpleRouter()
router.register("resource", SomeResourceViewSet, basename="resource")
urlpatterns = [
# patched routes, see patched view above; this item MUST come first
path("v1/resource", fixed_resource_list_view, name="resource-list"),
# now we can include the other regular views generated by the DRF router
path("v1", include(router.urls)),
] Maybe there's some kind of work around to override the views generated by the |
Beta Was this translation helpful? Give feedback.
@adamckay we ended up going with a bit of a work around. Our use case is a bulk update endpoint at something like
PATCH /v1/list-resource
We have the normal viewset class like
Apparently the
ViewSet.as_view
method allows passing in a dict of action names to method handlers, so we ended up generating a custom view from the viewset likeThen assign this view it as the first item in
urlpatterns
to override t…