django==3.2.4 djangorestframework==3.12.4
blog/serializer.py
from rest_framework import serializers
from .models import Blog
class BlogSerializer(serializers.ModelSerializer):
class Meta:
model = Blog
fields = '__all__'
blog/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
blog/url.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\u extra\u actions” 但当我使用普通的django URL路径时,它会成功运行
blog/url.py
urlpatterns = [
path('blog/', BlogListCreateView.as_view(), name="blog"),
path('blog/<int:pk>', BlogRetrieveUpdateDestroyView.as_view(), name="action"),
]
从文件:
因此,不需要路径为
'action/<int:pk>'
'action'
就足够了Routers用于来自Django的
Viewsets
,而不是generics
视图。例如:Here's如何使用
generics
进行此操作相关问题 更多 >
编程相关推荐