I noticed that sites such as SO and Reddit use the url structure <basename>/<pk>/<slug>/ to route to the detail view of their posts, which makes me think that this should be the standard. Is there a way to accomplish this with django-rest-framework using DefaultRouter and ModelViewset?
example views.py:
class PostViewSet(viewsets.ModelViewSet):
...
lookup_fields = ['pk', 'slug']
example urls.py :
router = DefaultRouter()
router.register('posts', PostViewSet, basename='post')
app_name = 'api'
urlpatterns = [
path('', include(router.urls)),
]
URL routes:
/api/post/ api.views.PostViewSet api:post-list
/api/post/<pk>/<slug>/ api.views.PostViewSet api:post-detail
/api/post/<pk>/<slug>\.<format>/ api.views.PostViewSet api:post-detail
/api/post\.<format>/ api.views.PostViewSet api:post-list