1

I use django rest framework and have this error:

base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .model or .queryset attribute.

This is my code

urls.py

from django.conf.urls import patterns, include, url
from rest_framework import viewsets, routers
import views

router = routers.SimpleRouter()
router.register(r'book', views.BookViewSet.as_view())

views.py

from django.shortcuts import render_to_response
from mobileapp.models import Book
from rest_framework import generics
from mobileapp.serializers import BookSerializer


class BookViewSet(generics.ListAPIView):
    serializer_class = BookSerializer
    def get_queryset(self):
        queryset = Book.objects.filter(user=self.request.user)
        return queryset.order_by('-id')

serializers.py

from mobileapp.models import Book
from rest_framework import serializers


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ('id', 'url', 'date', 'comment')

2 Answers 2

1

Have you google'd error statement?

https://github.com/tomchristie/django-rest-framework/issues/933

http://django-rest-framework.org/api-guide/routers.html#usage

Sign up to request clarification or add additional context in comments.

5 Comments

I'v modefied urls.py: router.register(r'^book', views.BookViewSet.as_view(), base_name='book-list'). And now I have 404 error
with what url? try base_name = 'book', '-list' part is appended by SimpleRouter
url: localhost:8000/book
base_name = 'book' also returning 404
try localhost:8000/book/
1

I'v solved my problem. There is code.

urls.py

from django.conf.urls import patterns, include, url
from views import BookList, BookDetail
from rest_framework.urlpatterns import format_suffix_patterns

from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^book/$',  BookList.as_view(), name='book-list'),
    url(r'^book/(?P<pk>\d+)/$', BookDetail.as_view(), name='book-detail'),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'api'])

views.py

class BookList(generics.ListCreateAPIView):
    serializer_class = BookSerializer
    def get_queryset(self):
        queryset = Book.objects.filter(user=self.request.user)
        return queryset.order_by('-id')

class BookDetail(generics.RetrieveUpdateDestroyAPIView):
    model = Book
    serializer_class = BookSerializer

2 Comments

Not a very useful answer for other people
For everybody who does not see the solution taken here, the view "BookViewSet" isn't a Set so you got to register the url yourself, instead of registering it in a router instance. The router can only be used with sets. more here django-rest-framework.org/api-guide/routers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.