drf-extensions
drf-extensions copied to clipboard
'NestedRegistryItem' object has no attribute 'urls'
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
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.
@pbdeuchler, please, close the issue if @maryokhin's comment helped.
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 you were able to fix the issue?
I have not @auvipy, but I also haven't worked on this project in a while
the issue is valid then?
I would believe so