4
使用Django 1.6和REST框架2.路由一個REST資源作爲第二剩餘資源的子
我有一個URI
/rest/parent_resource/951
下面,我想提出一個第二資源作爲一個孩子這一資源,以創建,列表,獲取和刪除方法
/rest/parent_resource/951/child_resource
我是新和仍然在學習,我想這個網址配置:
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)),
及以下視圖集:
class ChildViewset(viewsets.ModelViewSet):
paginate_by = 100
queryset = diliModels\
.ChildModel\
.objects\
.exclude(status=0)\
.prefetch_related('parent_model')\
.all()
serializer_class = diliSerializers.MarketPlaceProposalBidSerializer
permission_classes = (MyAuth.MyAuthIsAuthenticated,)
def get_queryset(self):
queryset = None
parent_pk = self.kwargs['parent_resource_pk']
if proposal_pk is not None:
request = self.get_serializer_context()['request']
authenticatedUserId = request.user.user_id
queryset = self.queryset.filter(parent_model__id = parent_pk) & \
self.queryset.filter(parent_model__user__user_id = authenticatedUserId)
return queryset
我正在(以我在正確的方向前進,希望)錯誤:
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路由器生成的REGEX過於激進,並且路由到我的p arent_resource視圖集中和尋找一個@Action或@link ...
所以發生的是URL路由的路由這給parnet_resource試圖找到一個child_resource @action。所以問題是如何路由到子資源視圖集,而無需通過parent_resource viewset @action方法進行路由。