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

Router registry is not working as expected

Open mcosti opened this issue 2 years ago • 4 comments

Hey, thanks for this nice package! In our code we use this pattern:

from some_package.urls import some_other_router

main_router = DefaultRouter()
main_router.registry.extend(some_other_router.registry)

urlpatterns = [
    re_path(r"api/v1/", include(router.urls)),
]

The problem while using this package is that nested_router.registry does not contain the parent resource in the name, so the API will end up being /api/v1/nameservers

instead of

/api/v1/domain/<domain_pk>/nameservers

My hack-ish solution is this one:

NESTED_ROUTERS = [
    some_other_nested_router
]

for nested_router in NESTED_ROUTERS:
    router._urls = router.urls + nested_router.urls

But I would very much prefer if I could avoid this.

Thanks!

mcosti avatar Mar 01 '23 11:03 mcosti

Hi, @mcosti. Thanks for reporting it as an issue.

Yeah. I agree with you that would be better to not need to add the .urls together. That would also fix the root listing of resources.

Yet I could not figure out a clean way to do it after poking around DRF sourcecode for a while. If you get a solution for this, that would be very welcome :+1:

alanjds avatar Mar 01 '23 14:03 alanjds

Hey @alanjds . Could you maybe share what you tried and it didn't work?

Thanks

mcosti avatar Mar 01 '23 16:03 mcosti

This is a desirable improvement, would love to contribute if I can. Have either of you made headway on this?

Djones4822 avatar Oct 23 '23 04:10 Djones4822

I personally haven't, I just used the hack above in my project

mcosti avatar Oct 23 '23 08:10 mcosti