drf-extensions icon indicating copy to clipboard operation
drf-extensions copied to clipboard

'NestedRegistryItem' object has no attribute 'urls'

Open pbdeuchler opened this issue 9 years ago • 7 comments

Expected Routes:

/gardens/
/gardens/(garden_pk)/
/gardens/(garden_pk/notes/
/gardens/(garden_pk/notes/(note_pk)/
/gardens/(garden_pk)/plants
/gardens/(garden_pk)/plants/(plant_pk)/
/gardens/(garden_pk)/plants/(plant_pk)/notes/
/gardens/(garden_pk)/plants/(plant_pk)/notes/(note_pk)

Router:

# url router
router = ExtendedSimpleRouter()
garden_router = router.register(r'gardens', GardenViewSet, base_name='garden')
garden_router.register(r'notes', GardenNoteViewSet, 'gardens-note', parents_query_lookups=['gardens'])
plant_router = garden_router.register(r'plants', PlantViewSet, 'gardens-plant', parents_query_lookups=['gardens'])

plant_router.register(r'notes', PlantNoteViewSet, 'gardens-plants-note', parents_query_lookups=['gardens__plants'])
plant_router.register(r'metrics', PlantMetricViewSet, 'gardens-plants-metric', parents_query_lookups=['gardens__plants'])

With the error coming from:

urlpatterns = [
    url(r'^$', api_root, name='root'),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^docs/', include('rest_framework_swagger.urls')),
] + garden_router.urls + plant_router.urls + djoser_urls # THIS LINE

pbdeuchler avatar May 04 '15 07:05 pbdeuchler

The mistake is in this line:

garden_router = router.register(r'gardens', GardenViewSet, base_name='garden')

Here you get a route, not the router itself, returned by register().

Therefore, to avoid confusion, you should name it:

garden_routes = router.register(r'gardens', GardenViewSet, base_name='garden')

and then do router.urls instead of garden_router.urls + plant_router.urls. Hope that helps.

maryokhin avatar May 04 '15 09:05 maryokhin

@pbdeuchler, please, close the issue if @maryokhin's comment helped.

chibisov avatar May 05 '15 08:05 chibisov

Sorry for taking so long to get back to this... using this method apparently effects both regex groups when I set parent_query_lookups with registering on plant_routes, so I'm getting this error:

ImproperlyConfigured at /v1/
"^gardens/(?P<parent_lookup_gardens__plants>[^/.]+)/plants/(?P<parent_lookup_gardens__plants>[^/.]+)/notes/$" is not a valid regular expression: redefinition of group name 'parent_lookup_gardens__plants' as group 2; was group 1

new code:

# url router
router = ExtendedDefaultRouter()
garden_routes = router.register(r'gardens', GardenViewSet, base_name='garden')
garden_routes.register(r'notes', GardenNoteViewSet, 'gardens-note', parents_query_lookups=['gardens'])
plant_routes = garden_routes.register(r'plants', PlantViewSet, 'gardens-plant', parents_query_lookups=['gardens'])

plant_routes.register(r'notes', PlantNoteViewSet, 'gardens-plants-note', parents_query_lookups=['gardens__plants'])
plant_routes.register(r'metrics', PlantMetricViewSet, 'gardens-plants-metric', parents_query_lookups=['gardens__plants'])

pbdeuchler avatar May 26 '15 04:05 pbdeuchler

@pbdeuchler you were able to fix the issue?

auvipy avatar Mar 31 '16 07:03 auvipy

I have not @auvipy, but I also haven't worked on this project in a while

pbdeuchler avatar Apr 04 '16 17:04 pbdeuchler

the issue is valid then?

auvipy avatar Apr 04 '16 17:04 auvipy

I would believe so

pbdeuchler avatar Apr 04 '16 17:04 pbdeuchler