I have a tv channel model and created a django-restframework viewlet that gives me a list and a detailed view from the box. At the top, I added two custom views of the same object, called all_events and now_and_next_event, as described here: Marking additional methods for routing . It works great so far.
class ChannelViewSet(viewsets.ModelViewSet): """ A viewset for viewing and editing channel instances. """ serializer_class = serializers.ChannelSerializer queryset = Channel.objects.all() @link() def now_and_next_event(self, request, pk): ''' Show current and next event of single channel. ''' ...
Now I would like to add a custom view, which is not a single-object view, but a list:
class CurrentEvents(generics.ListCreateAPIView): ''' Show current event of all channels. ''' model = Event serializer_class = serializers.EventSerializer def get(self, request): ...
When I turn off my viewport and add a manual url template for it, it also works. But I did not understand how to make them work with the same prefix "api / channel /", or the fact that I would like to get more, how to add a custom list class to my window.
Here are my urlletle patterns:
^api/channel/$ [name='channel-list'] ^api/channel/(?P<pk>[^/]+)/$ [name='channel-detail'] ^api/channel/(?P<pk>[^/]+)/all_events/$ [name='channel-all-events'] ^api/channel/(?P<pk>[^/]+)/now_and_next_event/$ [name='channel-now-and-next-event']
And I would like to access my list, for example:
^api/channel/current_events/$ [name='event-current']
django django-rest-framework
beluga.me
source share