django-rest-framework中的list_route和detail_route有什么不同? [英] what's different about list_route and detail_route in django-rest-framework?

查看:379
本文介绍了django-rest-framework中的list_route和detail_route有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

like标题,

django-rest-中的 list_route detail_route 有何不同框架?

,如果我想在URL xxx / books / 1 / 中获得 1

我怎么写 url.py views.py

like title,
what's different about list_route and detail_route in django-rest-framework?
if I want to get 1 in url xxx/books/1/,
how can I write url.py and views.py ?

推荐答案

@list_route @detail_route 是可以添加到 ViewSet 的额外操作。两者都在视图集中提供了自定义路由功能。 ViewSet 上用 @detail_route @list_route 也将被路由。 list_route 将提供所有相关记录,而 detail_route 仅提供特定记录。例如,在UserViewSet类上给定这样的方法:

@list_route and @detail_route are extra actions that we can add to a ViewSet. Both provide a custom routing facility in view set. Any methods on the ViewSet decorated with @detail_route or @list_route will also be routed. list_route will give all the related records, whereas detail_route will provide only a particular record. For example, given a method like this on the UserViewSet class:

class UserViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
    def set_password(self, request, pk=None):

还将另外生成以下URL模式:

The following URL pattern would additionally be generated:

URL pattern: ^users/{pk}/set_password/$ Name: 'user-set-password'

有关路由器的更多信息,您可以访问 Django Rest Rramewrok文档在路由器上

For more information on routers you can visit the official Django Rest Rramewrok documentation on routers.

如果要获取 xxx / books / 1 / ,则您的 url.py views.py 应该看起来像这样。

If you want get xxx/books/1/ then your url.py and views.py should look like this.

urls.py

url(r'^xxx/books/(?P<id>[0-9]+)$', views.myview)

views.py

@csrf_exempt
def myview(request , id):

这篇关于django-rest-framework中的list_route和detail_route有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆