Django REST Framework API schema generation with same URL prefix - django

I'm documenting my Django application using get_schema_view() method, but when I open the Swagger UI all the URLs are placed under the same group.
My urls.py look like:
router = routers.DefaultRouter()
router.register(r'stores', StoreViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(router.urls)),
path('api/register/', UserRegistrationView.as_view(), name='register'),
path('api/login/', views.obtain_auth_token, name='login'),
# Documentation
path('openapi/', get_schema_view(
title='Title API',
description='Some description goes here...',
version='v0.1'
), name='openapi-schema'),
path('docs/', TemplateView.as_view(
template_name='documentation.html',
extra_context={'schema_url': 'openapi-schema'}
), name='docs')
]
All the endpoints are placed under a group called api in the Swagger UI.
How can I specify the api/ prefix while generating the schema in order to group the endpoints by model?
Thanks.

I had the same problem. The simple solution is to define prefix in settings.
SPECTACULAR_SETTINGS = {
'SCHEMA_PATH_PREFIX': r'/api/v[0-9]',
}
Answered here : Custom Grouping on OpenAPI endpoints with Django Rest Framework

Related

How to show other paths in the router root view of Django Rest Framework?

As per the example below, the second 'path' which uses 'customauth.urls' isn't visible in the Django Rest Framework's browsable API root view. I also want to add single API views to be shown in the API root view so people can see those views. But it doesn't seem possible. How to do it?
...
router = routers.DefaultRouter()
router.registry.extend(articleRouter.registry)
urlpatterns = [
path('', include(router.urls)),
path('custom-auth/', include('customauth.urls')), # THIS DOESN'T APPEAR IN THE API ROOT VIEW
]
...

i am facing issue to add path to product urls.py file ,i dont know how

```
I am creating CRUD for categories I make a CategoriesViewSet.
on the other hand, I register the router as default in urls.py(Products) for viewset of categories but I don't know how to add a path into the product => urls.py and also the path I want to include into the core url.py file.
product => urls.py
router =routers.DefaultRouter()
router.register(r'categories', CategoryViewSet)
urlpatterns = [
path('list/',ProductList.as_view(),name="View Product"),
path('add/',AddProduct.as_view(),name="Create Product"),
path('<int:pk>/',ProductDetails.as_view(),name="product_detail"),
# path('categories/', CategoryViewSet.as_view({'get':'list'}))
path(r'^', include(router.urls)),
re_path(r'categories/', CategoryViewSet.as_view({'get':'list'}),
name='product-detail')
]
Core => urls.py
path('admin/', admin.site.urls),
path('api/product/', include('products.urls')),
path('api/user/', include('user.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = format_suffix_patterns(urlpatterns)
I don't think you have a clear idea of what Routers do in Django.
From DRF's official documentation:
Some Web frameworks such as Rails provide functionality for
automatically determining how the URLs for an application should be
mapped to the logic that deals with handling incoming requests.
REST framework adds support for automatic URL routing to Django, and
provides you with a simple, quick and consistent way of wiring your
view logic to a set of URLs.
This line:
router.register(r'categories', CategoryViewSet)
according to Django REST Framework's (DRF) documentation, generates 2 URL patterns:
categories/ - Return the list of categories
categories/{pk}/ - Return category with specified primary key (pk)
You don't need to add those again in Product's urls.py. You can either only specify the router.register(...) method, or manually add them like that:
path('categories/', CategoryViewSet.as_view(), name='product-detail')
It works. In my view I implemented and it works that's why I am asking First of all we have to add the
import [re_path and include] in the Urls.py (Products)
then we have to add--------------------
~the code will be added in the urls.py(Products) below the import library.
router = routers.SimpleRouter()
router.register(r'', CategoryViewSet, 'categories')
in Url Patterns~
re_path(r'^categories/',include((router.urls,'categories'),namespace='categories'))
it works.

Can Django admin url be on a swagger file using drf-yasg?

I am deploying a Django API on Google Endpoint.
Google endpoint needs a documentatin generated with openapi2.0 to set all endpoint. If an url is not defined on the yaml file generated by openapi (here by drf-yasg) the endpoint can not be reached even if it is available.
Thing is, it would be very very very useful for me to access my django admin zone on my api but when I generate my yaml file it skips my admin url.
here is my url, basic:
urlpatterns = [
# back
path('', include('api.urls')),
path('', include(spectacular_url)),
path('admin/', admin.site.urls),
]
Do you know how to do this ?
Thanks

Remove user endpoints in Django rest auth

I am using Django rest auth for user account handling. For updating user info i have created custom endpoints, So i don't need the djnago-rest-auth generated endpoints /rest-auth/user/ (GET, PUT, PATCH). How can i remove these endpoints?
urls.py
urlpatterns = [
path('', include("rest_auth.urls"), name="user-auth"),
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
Edit
I want to use all other urls of rest-auth like login, register, etc. But i just dont want the /rest-auth/user/ as described here.
#bodoubleu 's answer didn't work, So i added them manually.
from rest_auth.views import (
LoginView, LogoutView, PasswordChangeView,
PasswordResetView, PasswordResetConfirmView
)
urlpatterns = [
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('login', LoginView.as_view(), name="user-login"),
path('logout', LogoutView.as_view(), name='user-logout'),
path('password/change/', PasswordChangeView.as_view(), name='rest_password_change'),
path('password/reset', PasswordResetView.as_view(), name='rest_password_reset'),
path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
Untested but this should work.
urlpatterns = [
path('user/', django.views.defaults.page_not_found),
path('', include("rest_auth.urls"), name="user-auth"),
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
If not you can manually define all the rest_auth.urls in your url patterns

Django Rest Swagger -> No Authentication + multiple apps

We are looking to use django swagger for generating REST API docs. Our app consists of many sub apps and all views use JWT authentication. Document seems very lacking and just mentions the adding of url. This shows me error 400 : ["The schema generator did not return a schema Document"] http://10.0.0.61:8001/API/?format=openapi
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title="HashCove REST API")
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'info/', BackendVersion.as_view()),
url(r'^transaction/', include('kyc_rest_services.kyc_connect_transaction_manager.urls')),
url(r'^drive/', include('kyc_rest_services.carbon_drive.urls')),
url(r'^channels/', include('kyc_rest_services.channels.urls')),
url(r'^accounts/', include('kyc_rest_services.kyc_connect_accounts.urls')),
url(r'^API/', schema_view, name="docs")
]
Can it show all the APIs in the other apps and no authentication required.