我正在使用Django REST API框架进行一个项目。当我在本地运行该项目时,我会得到以下错误:
File "/home/asad/PycharmProjects/microshop/venv/lib/python3.8/site-packages/rest_framework/routers.py", line 153, in get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: type object 'CustomerListAPIViewSet' has no attribute 'get_extra_actions'
我在谷歌和StackOverflow上搜索,但没有找到任何解决方案。
serializers.py
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ['name', 'phone_number', 'address', ]
views.py
class CustomerListAPIViewSet(generics.ListAPIView):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
urls.py
from django.urls import path, include
from accounts_app.views import CustomerListAPIViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'customer/', CustomerListAPIViewSet)
urlpatterns = router.urls
发布于 2020-05-10 09:01:21
你的问题在于你的观点的命名。这会造成混乱。
您实际上正在创建一个APIView
子类。但您将其命名为ViewSet
。您可以在ViewSet
路由器而不是APIView
路由器上注册APIView
类。
您可以执行以下更改,以使视图可运行。
视图文件:
class CustomerListAPIView(generics.ListAPIView):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
URLS文件:
from django.urls import path, include
from accounts_app.views import CustomerListAPIView
urlpatterns = [
path(r'^customer/', CustomerListAPIView.as_view(), name='customer-list')
]
相反,定义一个实际的ViewSet类,但是结构会有所不同。
https://stackoverflow.com/questions/61709402
复制相似问题