drf-nested-routers RuntimeError('未找到父级注册资源') [英] drf-nested-routers RuntimeError('parent registered resource not found')
问题描述
我试图利用drf-nested-routers软件包在我的API中创建嵌套路由.
I am attempting to utilize the package drf-nested-routers to created nested routes within my API.
我尝试遵循文档( https://github.com/alanjds/drf-nested-routers ),并通读多个Stackoverflow线程,以期解决此问题.
I've attempted to follow alongside the documentation (https://github.com/alanjds/drf-nested-routers) as well as read through multiple Stackoverflow threads in hopes to figure out this issue.
我想创建一个NestedSimpleRouter.这是到目前为止我的routers.py文件中的内容:
I would like to create a single NestedSimpleRouter. Here is what I have so far inside of my routers.py file:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework_nested import routers
from api_v1.viewsets import DeviceViewSet, BreadcrumbViewSet
router = DefaultRouter()
router.register(r'devices', DeviceViewSet, base_name='devices')
device_breadcrumbs_router = routers.NestedSimpleRouter(router, r'breadcrumbs', lookup='breadcrumb')
device_breadcrumbs_router.register(r'breadcrumbs', BreadcrumbViewSet, base_name='breadcrumbs')
api_url_patterns = [
path('', include(router.urls)),
path('', include(device_breadcrumbs_router.urls)),
]
然后将api_url_patterns
包含在我的urls.py文件中:
I then include the api_url_patterns
in my urls.py file:
from django.contrib import admin
from django.urls import path, include
from .routers import api_url_patterns
urlpatterns = [
path('api/v1/', include(api_url_patterns)),
path('admin/', admin.site.urls),
]
这是我的视图集:
class DeviceViewSet(viewsets.ModelViewSet):
serializer_class = DeviceSerializer
def get_queryset(self):
return Device.objects.all()
class BreadcrumbViewSet(viewsets.ModelViewSet):
serializer_class = BreadcrumbSerializer
def get_queryset(self):
device_id = self.kwargs.get('device', None)
return Breadcrumb.objects.filter(device_id=device_id)
希望有一个类似于/api/v1/devices/<device_id>/breadcrumbs/
的URL模式.不幸的是,我上面显示的代码导致错误RuntimeError('parent registered resource not found')
The hope is to have a URL pattern that looks like /api/v1/devices/<device_id>/breadcrumbs/
. Unfortunately, the code I have displayed above is resulting in the error RuntimeError('parent registered resource not found')
我似乎无法弄清楚为什么我提供的错误发生了.任何帮助将不胜感激.
I can't seem to figure out why this error is occurring with that I have provided. Any help would be much appreciated.
推荐答案
更改此行
device_breadcrumbs_router = routers.NestedSimpleRouter(router, r'breadcrumbs', lookup='breadcrumb')
到
device_breadcrumbs_router = routers.NestedSimpleRouter(router, r'devices', lookup='breadcrumb')
从DRF嵌套路由器的文档中
From DRF nested router's docs
router = routers.SimpleRouter()
router.register(r'domains', DomainViewSet)
domains_router = routers.NestedSimpleRouter(router, r'domains', lookup='domain')
domains_router.register(r'nameservers', NameserverViewSet, base_name='domain-nameservers')
注意r"domains"
的匹配方式.该错误试图说明它找不到添加嵌套资源的URL.它正在寻找/readcrumbs,但是不存在.
Notice how the r"domains"
match. The error is trying to say that it couldn't find a url to add the nested resources to. It was looking for a /breadcrumbs but that didn't exist.
这篇关于drf-nested-routers RuntimeError('未找到父级注册资源')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!