proposal: DJA DefaultRouter
See original GitHub issueProposal: rest_framework_json_api.routers.DefaultRouter
Current method of declaring URLPatterns
The current typical usage example is like this:
from rest_framework import routers
router = routers.DefaultRouter(trailing_slash=False)
router.register(r'blogs', BlogViewSet)
router.register(r'entries', EntryViewSet)
urlpatterns = [
# basic collection and item URLs:
url(r'^', include(router.urls)),
# relationships URLs:
url(r'^entries/(?P<pk>[^/.]+)/relationships/(?P<related_field>\w+)',
EntryRelationshipView.as_view(),
name='entry-relationships'),
url(r'^blogs/(?P<pk>[^/.]+)/relationships/(?P<related_field>\w+)',
BlogRelationshipView.as_view(),
name='blog-relationships'),
# related URLs:
url(r'entries/(?P<entry_pk>[^/.]+)/blog',
BlogViewSet.as_view({'get': 'retrieve'}),
name='entry-blog'),
]
URLPatterns created by the Default Router
The above creates the following URLPatterns for router.register(r'blogs', BlogViewSet)
:
00 = {URLPattern} <URLPattern '^blogs$' [name='blog-list']>
default_args = {dict} {}
lookup_str = {str} 'example.views.BlogViewSet'
name = {str} 'blog-list'
pattern = {RegexPattern} ^blogs$
_is_endpoint = {bool} True
_regex = {str} '^blogs$'
_regex_dict = {dict} {}
converters = {dict} {}
name = {str} 'blog-list'
regex = {SRE_Pattern} re.compile('^blogs$')
01 = {URLPattern} <URLPattern '^blogs\.(?P<format>[a-z0-9]+)/?$' [name='blog-list']>
default_args = {dict} {}
lookup_str = {str} 'example.views.BlogViewSet'
name = {str} 'blog-list'
pattern = {RegexPattern} ^blogs\.(?P<format>[a-z0-9]+)/?$
_is_endpoint = {bool} True
_regex = {str} '^blogs\\.(?P<format>[a-z0-9]+)/?$'
_regex_dict = {dict} {}
converters = {dict} {}
name = {str} 'blog-list'
regex = {SRE_Pattern} re.compile('^blogs\\.(?P<format>[a-z0-9]+)/?$')
02 = {URLPattern} <URLPattern '^blogs/(?P<pk>[^/.]+)$' [name='blog-detail']>
default_args = {dict} {}
lookup_str = {str} 'example.views.BlogViewSet'
name = {str} 'blog-detail'
pattern = {RegexPattern} ^blogs/(?P<pk>[^/.]+)$
_is_endpoint = {bool} True
_regex = {str} '^blogs/(?P<pk>[^/.]+)$'
_regex_dict = {dict} {}
converters = {dict} {}
name = {str} 'blog-detail'
regex = {SRE_Pattern} re.compile('^blogs/(?P<pk>[^/.]+)$')
03 = {URLPattern} <URLPattern '^blogs/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$' [name='blog-detail']>
default_args = {dict} {}
lookup_str = {str} 'example.views.BlogViewSet'
name = {str} 'blog-detail'
pattern = {RegexPattern} ^blogs/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$
along with an api-root
view that lists the available registry entries. This is a nice feature.
Drawbacks of the current method
- Relationships & related URLs follow a standard pattern yet have to be “manually” declared separately. This is hard to do, error prone and just plain annoying.
- We might not need the
.<format>
flavor of each ViewSet. (Not a huge concern.)
Proposed: rest_framework_json_api.DefaultRouter
Create a DJA DefaultRouter that extends DRF’s DefaultRouter:
- Add URLPatterns for relationships and their
RelationshipView
. This seems pretty straightforward. Just add another argument toDefaultRouter.register()
(or apply a default naming pattern) to add the RelationshipView. - Add URLPatterns for related links. This is a bit harder as each related link has to be enumerated (link name and ViewSet).
The goal would be to have a URLPatterns that looks like this:
from rest_framework_json_api import routers
router = routers.DefaultRouter(trailing_slash=False)
router.register(r'blogs', BlogViewSet, BlogRelationshipView)
router.register(r'entries', EntryViewSet, EntryRelationshipView, ??related stuff here??)
urlpatterns = [
url(r'^', include(router.urls)),
]
For how to do the “related stuff here” take a look at @Anton-Shutik’s approach in #451
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Routers - Django REST framework
DefaultRouter. This router is similar to SimpleRouter as above, but additionally includes a default API root view, that returns a response containing ...
Read more >Django Rest Framework File Upload - Stack Overflow
We'll be using the defaultRouter to add CRUD urls for our viewset and we'll add one more fixed url specifying the uploader view...
Read more >Building a JSON:API Backend with Django REST Framework ...
Create a new Python project with a virtual environment: ... let's set up Django REST Framework JSON API (DJA) so we can access...
Read more >Discussions · django-json-api/django-rest-framework-json-api
DJA to be part of Hacktoberfest · sliverc started on Sep 29, 2021 in General · @sliverc · @auvipy · @sliverc · @auvipy...
Read more >How to Build an API in Python (with Django) - RapidAPI
Today in this tutorial, we're going to cover the following: How to start a project in Django How to deliver JSON to a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A PR is certainly welcome in this regard
Is this something the project is still interested in? When I started using DJA, I found it a little frustrating and unintuitive that we couldn’t just “automatically” include the relationship and related resource routes.
If a PR is likely to be accepted, I could look into this within the next 1-2 weeks.