Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个rest资源路由为第二个rest资源的子资源_Python_Django_Rest_Django Rest Framework - Fatal编程技术网

Python 将一个rest资源路由为第二个rest资源的子资源

Python 将一个rest资源路由为第二个rest资源的子资源,python,django,rest,django-rest-framework,Python,Django,Rest,Django Rest Framework,使用Django 1.6和Rest框架2 我有一个uri /rest/parent_resource/951 下面,我想用create、list、get和delete方法将第二个资源表示为该资源的子资源 /rest/parent_resource/951/child_resource 我是新手,仍在学习,我尝试了以下url配置: router.register(r'parent_resource', diliMarketplace.MarketPlaceProposalViewSet_

使用Django 1.6和Rest框架2

我有一个uri

 /rest/parent_resource/951
下面,我想用create、list、get和delete方法将第二个资源表示为该资源的子资源

 /rest/parent_resource/951/child_resource
我是新手,仍在学习,我尝试了以下url配置:

 router.register(r'parent_resource', diliMarketplace.MarketPlaceProposalViewSet_Internal)
然后注册我添加的路由器:

 url(r'^rest/parent_resource/(?P<parent_resource_pk>[0-9]+)/child_resource/$', ChildViewset.as_view())
url(r'^rest/', include(router.urls)),
我遇到的错误(希望我的方向正确):

我认为URL路由器生成的正则表达式太过激进,正在路由到我的父资源视图集并寻找@Action或@Link



所以现在发生的是URL路由将其路由到parnet_资源,试图找到一个子_资源@action。因此,问题是如何路由到子资源视图集,而不通过父资源视图集@action方法进行路由。

真正深入了解源代码并重新阅读教程,让我对URL配置有了更好的理解

url(r'^rest/parent_resource/(?P<parent_resource_pk>[0-9]+)/child_resource/$', ChildViewset.as_view({
        'get': 'list',
        'post': 'create'
    }))
url(r'^rest/parent\u resource/(?P[0-9]+)/child\u resource/$),ChildViewset.as\u视图({
“获取”:“列表”,
“post”:“create”
}))
我还需要列出as_view方法中的方法

Internal Server Error: /rest/parent_resource/951/child_resource/
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "C:\Python27\lib\site-packages\rest_framework\viewsets.py", line 69, in view
    for method, action in actions.items():
AttributeError: 'NoneType' object has no attribute 'items'
url(r'^rest/parent_resource/(?P<parent_resource_pk>[0-9]+)/child_resource/$', ChildViewset.as_view({
        'get': 'list',
        'post': 'create'
    }))