drf-nested-routers icon indicating copy to clipboard operation
drf-nested-routers copied to clipboard

Facing runtime error

Open sodrooome opened this issue 6 years ago • 1 comments

I was wondering if I could get your help on this, so i would like to create a single nested simple router but unfortunately, it's result the error like RuntimeError: parent registered resource not found.

here's my urls.py which contains the following code like this:

router = routers.DefaultRouter()
router.register(r'accounts', AccountViewSet) 
router.register(r'projects', ProjectViewSet)

accounts_router = routers.NestedSimpleRouter(router, r'accounts', lookup='account')
accounts_router.register(r'projects', AccountProjectsViewSet)

I can't seem to figure out why this error is still occurring, any help would be much appreciated

sodrooome avatar Mar 17 '19 15:03 sodrooome

going to add to this topic, as it seems related - at this moment, it seems like NestedDefaultRouter is not working properly too. Or maybe missing its whole "DefaultRouter" purpose.

Doing things the following way gives me the very same RuntimeError OP has got:


from rest_framework_nested import routers

router = routers.DefaultRouter()

category_router = routers.NestedDefaultRouter(
    parent_router=router,
    parent_prefix="category",
)

Doing things the following way kinda work:


from rest_framework_nested import routers

from views import TestViewSet, Foo, Bar

router = routers.DefaultRouter()

router.register(
    prefix="test",
    viewset=TestViewSet,
)

category_router = routers.NestedDefaultRouter(
    parent_router=router,
    parent_prefix="test",
)

category_router.register(
    prefix="foo",
    viewset=Foo,
)
category_router.register(
    prefix="bar",
    viewset=Bar,
)

I mean - router itself now works, I'm able to get to /test/ endpoint. However, it does not work as a default router.

What I've expected:

  • Entering /test/ gives me the same layout as DefaultRouter usually does, just for endpoints registered to category_router. E.g /test/foo/ and /test/bar/.

What I've got:

  • Entering /test/ renders the TestViewSet. I'm able to access /test/foo/ and /test/bar/, but miss the APIRootView

Am I doing it wrong, or? How do I correctly get a DefaultRouter-like behavior (router that renders a list of endpoints attached to it on its base endpoint) on a nested router?

moonburnt avatar Nov 29 '22 00:11 moonburnt