gpt4 book ai didi

python - Django REST Framework @detail_route 未按预期工作

转载 作者:行者123 更新时间:2023-11-28 21:49:59 25 4
gpt4 key购买 nike

我无法让@detail_route 正常工作。

我有两个 API 调用要处理:

/movie/
/movie/highlight

我正在尝试使用 @detail_route 来获取 View 集中的/movie/highlight url。

我的 urls.py 看起来像这样:

from django.conf.urls import url, include
from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router.register(r'users', views.UsersViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'movie', views.MovieViewSet)

我的 views.py 看起来像这样:

rom django.contrib.auth.models import User, Group
from movies.models import Movie

from api.serializers import UserSerializer, GroupSerializer, MovieSerializer

from rest_framework.response import Response
from rest_framework import permissions
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.decorators import detail_route

# Code from DRF quickstart tutorial
class UsersViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer

# Code from DRF quickstart tutorial
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer

# MY CODE
class MovieViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Movie.objects.all().order_by('-title')
serializer_class = MovieSerializer

@detail_route(renderer_classes=(renderers.StaticHTMLRenderer,))
def highlight(self, request, *args, **kwargs):
snippet = "Highlight"
return Response(snippet)

serializers.py 看起来像这样:

from django.contrib.auth.models import User, Group
from movies.models import *
from rest_framework import serializers

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')

# My code...
class MovieSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Movie
fields = ('title', 'details')

如果我尝试/movie/url API 调用,它会按预期工作。如果我尝试/movie/highlight/调用,我会收到 404 错误。

我是 DRF 的新手,所以怀疑我在这里做了一些非常愚蠢的事情,但无法从我扫描的各种文档和教程中找到什么。

最佳答案

您需要使用@list_route 装饰器而不是@detail_route 装饰器。

这将生成 url movie/highlight/

使用@list_route修饰的方法生成{prefix}/{methodname}/类型的url,而detail_route修饰的方法生成类型的url {前缀}/{查找}/{方法名}/。这里 methodname 是您的方法的名称,lookup 是执行查找以获取详细 View 对象的查找值。

class MovieViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Movie.objects.all().order_by('-title')
serializer_class = MovieSerializer

# use list_route decorator
@list_route(renderer_classes=(renderers.StaticHTMLRenderer,))
def highlight(self, request, *args, **kwargs):
snippet = "Highlight"
return Response(snippet)

关于python - Django REST Framework @detail_route 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568484/

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