Django error with ViewSet using SimpleRouter
<p>views.py</p>

<pre><code>from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import generics
from ticker.serializers import PriceSerializer
from ticker.models import Price
import datetime
from nexchange.settings import DEFAULT_HOUR_RANGE


class LastPricesViewSet(viewsets.ViewSet):
    def list(self, request):
        queryset = Price.objects.filter().order_by('-id')[:2]
        serializer = PriceSerializer(queryset, many=True)
        return Response(serializer.data)


class PriceHistoryViewSet(generics.ListAPIView):
    serializer_class = PriceSerializer

    def get_queryset(self, request):
        hours = self.request.query_params.get('hours', DEFAULT_HOUR_RANGE)
        relevant = datetime.datetime.now() - datetime.timedelta(seconds=hours * 3600)
        queryset = Price.objects.filter(created_on__gte=relevant).order_by('id')
        return queryset
</code></pre>

<p>urls.py:</p>

<pre><code>from rest_framework.routers import SimpleRouter
from ticker.views import LastPricesViewSet, PriceHistoryViewSet

router = SimpleRouter()
router.register(r'price/latest', LastPricesViewSet, base_name='latest')
router.register(r'price/history', PriceHistoryViewSet, base_name='history')
api_patterns = router.urls
</code></pre>

<p>The following error is raised during <code>runserver</code> (without basename kwarg):
<code>AssertionError:</code>base_name<code>argument not specified, and could not automatically determine the name from the viewset, as it does not have a</code>.queryset<code>attribute.</code></p>

<p>However, when I add the wished <code>basename</code>, the error changes to:
<code>TypeError: as_view() takes 1 positional argument but 2 were given</code></p>

<p>I suspect that it might be related to combining a ViewSet and a ListAPIView in one router.</p>

<p>Solution:
You must inherit from <code>viewsets.ViewSetMixin</code> to implement the methods that are required by a ViewSetClass to be registered with the DRF router, otherwise it is possible to use the simple Django <code>urlconf</code> notation.</p>

<p><code>views.py:</code></p>

<pre><code>class PriceHistoryViewSet(viewsets.ViewSetMixin, generics.ListAPIView):
    serializer_class = PriceSerializer

    def get_queryset(self, *args, **kwargs):
        hours = self.request.query_params.get('hours', DEFAULT_HOUR_RANGE)
        relevant = datetime.datetime.now() - datetime.timedelta(seconds=hours * 3600)
        queryset = Price.objects.filter(created_on__gte=relevant).order_by('id')
        return queryset
</code></pre>

<p><code>urls.py:</code></p>

<pre><code>from rest_framework.routers import SimpleRouter
from ticker.views import LastPricesViewSet, PriceHistoryViewSet

router = SimpleRouter()
router.register(r'price/latest', LastPricesViewSet, base_name='latest')
router.register(r'price/history', PriceHistoryViewSet, base_name='history')
api_patterns = router.urls
</code></pre>

python django-rest-framework