简体   繁体   中英

Api root on django rest framework

I am newbie on DRF so I wonder if there a "elegant" way to add more endpoints to API Root. I have an endpoint to manage my users and another to manage their posts, but on http://localhost:8000/api/ only profiles appears. I search for some solutions that implies change the URL but I don't want that. For example my current endpoint to get all posts is localhost:8000/api/posts that looks fine. I don't want something like api/posts/posts or similar. Is there any alternative?

    {
    "profiles": "http://localhost:8000/api/profiles/"
}

main urls.py

urlpatterns = [
    path('docs/', schema_view.with_ui('swagger',
                                         cache_timeout=0), name='schema-swagger-ui'),
    path('admin/', admin.site.urls),
    path('api/', include('profiles.urls')),
    path('api/', include('posts.urls')),
    path('api/', include('comments.urls')),
    path('api/', include('private_messages.urls')),
]

userprofile urls.py

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework_simplejwt import views as jwt_views
from .views import UserProfileViewSet

router = DefaultRouter()
router.register('profiles', UserProfileViewSet)

urlpatterns = [
    path('', include(router.urls)),
    path('auth/login', jwt_views.TokenObtainPairView.as_view()),
    path('auth/refresh', jwt_views.TokenRefreshView.as_view()),
]

posts urls.py

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PostViewSet, PostByAuthorViewSet

router = DefaultRouter()
router.register('posts', PostViewSet)

urlpatterns = [
    path('posts-by-author/<author_id>', PostByAuthorViewSet.as_view()),
    path('', include(router.urls))
]

but on http://localhost:8000/api/ only profiles appears

This is the expected behavior. Django is returning the first matching path from your URL configuration file, which is the profiles module in your case. Your duplicate api/ paths are ignored.

If you really want everything to be in /api , you'd have to define all your URLs in the base urls.py file. However, I would highly recommend you keep your current structure and add the module name to your endpoints:

urlpatterns = [
    path('docs/', schema_view.with_ui('swagger',
                                         cache_timeout=0), name='schema-swagger-ui'),
    path('admin/', admin.site.urls),
    path('api/profiles/', include('profiles.urls')),
    path('api/posts/', include('posts.urls')),
    path('api/comments/', include('comments.urls')),
    path('api/private_messages/', include('private_messages.urls')),
]

Otherwise your urls.py file can get messy very quickly as your project grows.

If a route like localhost:8000/api/posts/posts is bothering you, you should modify the URL configuration in your posts app. Something like this would probably work better for you:

from django.urls import path
from .views import PostViewSet, PostByAuthorViewSet

urlpatterns = [
    path('by-author/<author_id>', PostByAuthorViewSet.as_view()),
    path('', PostViewSet.as_view())
]

This way you'd have the routes localhost:8000/api/posts/ and localhost:8000/api/posts/by-author/1 . As a side note, you should probably look into filters , since posts-by-authors sounds like a viewset that can easily be included in your PostViewSet with a query parameter.

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