AttributeError:'模块'对象没有属性' ListCreateAPIVIEW' Django的

时间:2016-03-31 11:43:59

标签: python django django-rest-framework

我正在使用' Django Rest Framework'我正在尝试构建RestfulAPI。但是,当我尝试“制作色彩”时,我会收到上述错误。即同步数据库。

这是我的模特:

from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Animal(models.Model):
    id = models.CharField(max_length=10, primary_key=True)
    name = models.CharField(max_length=200)
    gender = models.CharField(max_length=10)
    breed = models.CharField(max_length=200)
    adoption = models.CharField(max_length=10)
    vaccines = models.CharField(max_length=20)

class Doctor(models.Model):
    id= models.CharField(max_length=10, primary_key=True)
    name = models.CharField(max_length=20)

以下是我的观点:

from django.contrib.auth.models import User, Group
from rest_framework import viewsets, generics

from cw.myStart.models import Animal
from cw.myStart.serializers import UserSerializer, GroupSerializer, AnimalSerialiser, DoctorSerealiser
from models import Animal, Doctor

class AnimalList(generics.ListCreateAPIVIEW):
   queryset = Animal.objects.all()
   serializer_class = AnimalSerialiser

class DoctorDetail(generics.RetrieveUpdateDestroyAPIView):
   queryset = Doctor.objects.all()
   serializer_class = DoctorSerealiser

这是我的urls.py:

from django.conf.urls import url, include
from rest_framework import routers
from rest_framework.urlpatterns import format_suffix_patterns
from cw.myStart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/$', views.AnimalList.as_view()),
    url(r'^api/(?P<pk>[0-9]+)/$', views.AnimalDetail.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)

提前致谢。

1 个答案:

答案 0 :(得分:3)

您应该使用ListCreateAPIView而不是ListCreateAPIVIEW

替换它:

class AnimalList(generics.ListCreateAPIVIEW):

有了这个:

class AnimalList(generics.ListCreateAPIView):

请参阅django-rest-framework docs