gpt4 book ai didi

python - django-rest-framework "AttributeError: ' 函数'对象没有属性 'get_extra_actions'"

转载 作者:行者123 更新时间:2023-12-04 07:30:18 24 4
gpt4 key购买 nike

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
博客/网址.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 路径时,它运行成功
博客/网址.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, weactually don't need to design the URL conf ourselves. The conventionsfor wiring up resources into views and urls can be handledautomatically, using a Router class.


所以你不需要路径是 'action/<int:pk>' . 'action'就足够了。

关于python - django-rest-framework "AttributeError: ' 函数'对象没有属性 'get_extra_actions'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67970159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com