django==3.2.4 djangorestframework==3.12.4
博客/serializer.py
from rest_framework import serializers
from .models import Blog
class BlogSerializer(serializers.ModelSerializer):
class Meta:
model = Blog
fields = '__all__'
博客/views.py
from rest_framework import generics
from .models import Blog
from .serializers import BlogSerializer
class BlogListCreateView(generics.ListCreateAPIView):
queryset = Blog.objects.all()
serializer_class = BlogSerializer
class BlogRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Blog.objects.all()
serializer_class = BlogSerializer
博客/urls.py
router = DefaultRouter()
router.register(r'blogs', BlogListCreateView.as_view(), basename="blogs")
router.register(r'action/<int:pk>', BlogRetrieveUpdateDestroyView.as_view(), basename="action")
urlpatterns = [
path('', include(router.urls)),
]
当我使用路由器时,它显示AttributeError:'function'对象没有属性'get_extra_actions' 但是当我使用正常的 django urls 路径时它运行成功
博客/urls.py
urlpatterns = [
path('blog/', BlogListCreateView.as_view(), name="blog"),
path('blog/<int:pk>', BlogRetrieveUpdateDestroyView.as_view(), name="action"),
]
最佳答案
来自文档:
Because we're using ViewSet classes rather than View classes, we actually don't need to design the URL conf ourselves. The conventions for wiring up resources into views and urls can be handled automatically, using a Router class.
所以你不需要路径是 'action/<int:pk>'
。 'action'
就足够了。
关于python - django-rest-framework "AttributeError: ' 函数'对象没有属性 'get_extra_actions' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67970159/