I want to implement routes like this:
/items - list of all items.
/items/types - list of all item types
I was looking at drf-nester-routs, but nested urls expect {pk} to be passed. Is there any good way to achieve what i want?
I want to implement routes like this:
/items - list of all items.
/items/types - list of all item types
I was looking at drf-nester-routs, but nested urls expect {pk} to be passed. Is there any good way to achieve what i want?
If you do not need pk
, then your route should be /types
not /items/types
You may need to take a look at this SO question about REST nested resources:
With ID /items/1/types
would mean something like "display all types belonging to the item with id 1". Whereas /items/types
doesn't really make sense because resource types can't belong to all item resources.
However, you could implement it, as a custom action for your ViewSet using @list_route
decorator, e.g.
class MyViewSet(viewsets.ModelViewSet):
...
@list_route()
def types(self, request):
return Response(some_way_to_list_types())
...
It's probably not a RESTful way though.