简体   繁体   中英

urls with DRF DefaultRouter. How to add a prefix?

I'm using django-rest-framework to setup a RESTful API for my webapp. The problem is that I need to keep trace of edited content and to review them before publish. So I came up with creating two models (for instance Note and NoteEdit ). This is working quite fine but in urls.py I need to handle urls so if they match /api/edits/notes/ they are routered to NoteEditViewSet and if they match /api/notes/ they are routered to NoteViewSet .

# urls.py

from django.urls import include, path
from rest_framework import routers
from api_v1 import views
    
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'notes', views.NoteViewSet)
    
    
edits_router = routers.DefaultRouter()
router.register(r'notes', views.NoteEditViewSet)
    
urlpatterns = [
    path('edits/', include(edits_router.urls)),
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]

This is not working. Why?

NoteEditViewSet is still registered to router . Change it to:

edits_router = routers.DefaultRouter()
edits_router.register(r'notes', views.NoteEditViewSet)
# ^^^ change to this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM