alanjds / drf-nested-routers

Nested Routers for Django Rest Framework
https://pypi.org/project/drf-nested-routers/
Apache License 2.0
1.66k stars 157 forks source link

Facing runtime error #135

Open sodrooome opened 5 years ago

sodrooome commented 5 years ago

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

moonburnt commented 1 year ago

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:

What I've got:

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?