gpt4 book ai didi

Django Rest框架返回 "detail": "Not found."

转载 作者:行者123 更新时间:2023-12-02 09:23:45 26 4
gpt4 key购买 nike

当我尝试进行获取或部分更新时,我有一个 API 端点出现问题。我不确定我错过了什么,我已将代码包含在尝试及其给出的错误中。

错误:

"detail": "Not found."

据我了解,url 应该包含我想要部分更新的实例的 ID,因此执行 PATCH 的 url 是:

http://localhost:8000/manuscripts-api/manuscriptlibrary/9/

我的看法:

class ManuscriptLibraryViewSet(viewsets.ModelViewSet):
"""Handles creating, reading and updating manuscript library."""

authentication_classes = (TokenAuthentication,)
serializer_class = serializers.ManuscriptLibrarySerializer
queryset = models.ManuscriptLibrary.objects.all()
permission_classes = (permissions.PostOwnManuscriptLibrary,)

def put(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)

我的模型:

class ManuscriptLibrary(models.Model):
"""Represents a single manuscript's library"""

manuscript = models.OneToOneField(ManuscriptItem, on_delete=models.CASCADE)
bookmarks = models.CharField(max_length=99999999)
history = models.CharField(max_length=99999999)

def __str__(self):
"""Django uses when it needs to convert the object to a string"""
return str(self.manuscript)

我的序列化器:

class ManuscriptLibrarySerializer(serializers.ModelSerializer):
"""A serializer for a manuscript's library."""

class Meta:
model = models.ManuscriptLibrary
fields = ('id', 'manuscript', 'bookmarks', 'history')

我的网址:

router = DefaultRouter()
router.register('manuscripts', views.ManuscriptViewSet, base_name="manuscripts") # auto basename for models
router.register('manuscriptlibrary', views.ManuscriptLibraryViewSet, base_name="manuscript_library")
router.register('manuscriptsettings', views.ManuscriptSettingsViewSet)

urlpatterns = [
url(r'', include(router.urls))
]

我的许可:

class PostOwnManuscriptLibrary(permissions.BasePermission):
"""Allow author to update a manuscript's library they own."""

def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True
return obj.manuscript.author == request.user

def has_permission(self, request, view):
if request.method == 'POST':
manuscript = request.data.get('manuscript')
instance = ManuscriptItem.objects.get(pk=manuscript)
return instance.author == request.user
return True

其他信息:

网址:

http://localhost:8000/manuscripts-api/manuscriptlibrary/

返回:

[ { "id": 28, "manuscript": 9, "bookmarks": "test success", "history": "test success" } ]

最佳答案

对于修补程序并获取详细信息,您需要使用 id 中的值,在您的情况下,这是 28,尝试转到路径并修补

http://localhost:8000/manuscripts-api/manuscriptlibrary/28/

关于Django Rest框架返回 "detail": "Not found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057653/

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