3 ms·
This bit: > Flask > Django for APIs. For example, a nested api resource in Flask is > @app.route('/users/<string:user_id>/teams/<string:team_id>', methods=['G
by andybak 12y ago
This bit:
> Flask > Django for APIs. For example, a nested api resource in Flask is
> @app.route('/users/<string:user_id>/teams/<string:team_id>', methods=['GET', 'POST'])
> while with Django REST framework this is impossible.
> (drf-nested-routers helps but breaks unless you use only the most basic Django REST framework features.
Is this really true? For a start I've managed to build a complex app without needing nested routers (although they are cool). Second - I've used DRF extensions nested routers without finding any problems.
For me needing an API is a point in favour of using Django for a lot of use cases - you get an elegant declarative syntax and lots of batteries included.
- jhgg 12y agoIt's not true at all. I've done nested routers in DRF plenty of times. router = DefaultRouter() router.register('users/(?P<user_id>\d+)/teams', UserTeamViewSet) You can then access user_id via self.kwargs['user_id'] from within the viewset. And the team ID is implicitly added by the router to support list (and update), and retrieve (delete, put, patch) methods. user_id is also passed as a kwarg to the dispatched method (create, retrieve, etc...)
- asmosoinio 12y agoI don't get it either. The route definition is just an url with two arguments, right? Maybe I'm missing something...
- mpdehaan2 12y agoI ended up extending DJRF to do nested collections, but I'd argue your REST topology would be better with one canonical resource and unique team IDs: /teams/<id>/ It's fine to be able to add a user to a team by posting to a subcollection though, but there should only be one URL for the resource (IMHO).
- welder 12y agodrf-nested-routers only works with the most basic Fields. For example: https://github.com/alanjds/drf-nested-routers/issues https://github.com/alanjds/drf-nested-routers/issues Right now it's a hack that limits your api features, but might change in the future.