如何将命名空间 url 添加到 django-rest-framework 路由器视图集 [英] How to add namespace url to a django-rest-framework router viewset

查看:17
本文介绍了如何将命名空间 url 添加到 django-rest-framework 路由器视图集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的 api 路由器添加一个 url 命名空间,但是当我这样做时,路由器仍然会寻找没有命名空间的 url:

I would like to add a url namespace to my api router but when I do the router still looks for urls without a namespace:

router = DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'events', EventViewSet)
router.register(r'comments', CommentViewSet)

urlpatterns = patterns('apiroot.views',
                       url(r'^', include(router.urls, namespace='api')),
                       )

可浏览的 api 会寻找像 'user-list''user-detail' 这样的 url 名称,而不是 'api:user-list' 这是我希望发生的事情.

The browsable api looks for url names like 'user-list' and 'user-detail' still instead of 'api:user-list' which is what I would like to have happen.

我假设对此有一个简单的解决方法(这似乎是一件非常标准的事情),但我在 django-rest-framework 文档中找不到任何解决方案.如果我遗漏了文档中的某些内容,请分享一个链接,或者如果我处理这个错误(即你为什么要这样做,这是不好的做法"或其他什么)请解释为什么或管理 api url 的正确方法是什么是.谢谢!

I'm assuming there is an easy fix for this (it seems like a pretty standard thing to want to do) but I can't find any solution in the django-rest-framework docs. If I'm missing something from the docs, please share a link or if I am approaching this wrong (i.e "why would you do that, it's bad practice" or whatever) please explain why or what the correct way to manage api urls would be. Thanks!

推荐答案

有一个 当前为此打开拉取请求.请随时加入讨论.

There's a pull request open for this currently. Please feel free to join the discussion.

这篇关于如何将命名空间 url 添加到 django-rest-framework 路由器视图集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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