Django REST框架:类型对象X没有属性'get_extra_actions'

时间:2019-05-09 05:44:29

标签: python django django-rest-framework

Ubuntu LTS,Python 3.6.x,Django 2.2.1,DRF 3.9.3

我从DRF快速入门开始。工作正常。

跳到DRF tutorial, step 3 on using generic class based views时尝试manage.py runserver时遇到此错误

File "/home/ubuntu/src/rasi/rais/instances/urls.py", line 11, in <module>
    path('api/', include(router.urls)),
  File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 125, in
urls
    self._urls = self.get_urls()
  File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 386, in
get_urls
    urls = super(DefaultRouter, self).get_urls()
  File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 284, in
get_urls
    routes = self.get_routes(viewset)
  File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 200, in
get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: type object 'InstanceList' has no attribute 'get_extra_actions'

为了实现本教程中的建议,我仅对代码库进行了最小的更改。

我做错了事并不明显,当我阅读ListCreateAPIView指南时,get_extra_actions周围没有很多信息,而在其他地方看时似乎没有什么可比的 >

urls.py

from django.urls import include, path
from rest_framework import routers
from . import views

router = routers.DefaultRouter()
router.register(r'users',views.UserViewSet)
router.register(r'instances',views.InstanceList)                                                                            

urlpatterns = [ 
    path('', views.dash, name='dash'),
    path('api/', include(router.urls)),
]

models.py

class User(AbstractUser):
    email = models.EmailField()
    dept = models.CharField(max_length=400)
    display_name = models.CharField(max_length=200)

class Instance(models.Model):
    name = models.CharField(max_length=200)
    flavour = models.CharField(choices=FLAVOUR_CHOICES, max_length=20)
    os = models.IntegerField(choices=OS_CHOICES)
    hostname = models.CharField(max_length=200)
    allocation_manager = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name='manager')
    allocation_notes = models.TextField(blank=True,null=True)
    requested_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name='requestor')

serializer.py

# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):                                                               
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')

class InstanceSerializer(serializers.HyperlinkedModelSerializer):
    allocation_manager = UserSerializer()
    requested_by = UserSerializer()

    class Meta:
        model = Instance
        fields = ('name', 'flavour', 'hostname', 'allocation_manager','requested_by')

views.py

# Serializer Views
# ViewSets define the view behavior.

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class InstanceList(generics.ListCreateAPIView):
    queryset = Instance.objects.all()
    serializer_class = InstanceSerializer

注意/编辑:我找到了some questions, but few solutions

2 个答案:

答案 0 :(得分:5)

基本上,您正在尝试向路由器注册非视图集(此处为InstanceList视图,而不是视图集)。取而代之的是,您可以在以下网址中简单地使用它:

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

urlpatterns = [ 
    path('', views.dash, name='dash'),
    path('api/', include(router.urls)),
    path('api/instances/', views.InstanceList.as_view(), name="instances"),
]

答案 1 :(得分:1)

出现这个错误是因为: 您在 urls.py 和 ListAPIView 或其他通用视图中使用路由器 你应该使用 viewset.ModelViewSet 而不是通用的 api 视图。