chibisov / drf-extensions

DRF-extensions is a collection of custom extensions for Django REST Framework
http://chibisov.github.io/drf-extensions/docs
MIT License
1.48k stars 208 forks source link

'NestedRegistryItem' object has no attribute 'urls' #81

Open pbdeuchler opened 9 years ago

pbdeuchler commented 9 years ago

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
maryokhin commented 9 years ago

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.

chibisov commented 9 years ago

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

pbdeuchler commented 9 years ago

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'])
auvipy commented 8 years ago

@pbdeuchler you were able to fix the issue?

pbdeuchler commented 8 years ago

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

auvipy commented 8 years ago

the issue is valid then?

pbdeuchler commented 8 years ago

I would believe so