1

我使用django rest 框架并出现此错误:

base_name未指定参数,并且无法从视图集中自动确定名称,因为它没有.modelor.queryset属性。

这是我的代码

网址.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())

视图.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')

序列化程序.py

from mobileapp.models import Book
from rest_framework import serializers


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

2 回答 2

1

你有谷歌的错误声明吗?

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

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

于 2013-10-24T07:04:53.313 回答
0

我已经解决了我的问题。有代码。

网址.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'])

视图.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
于 2013-10-25T06:43:42.767 回答