rest framework swagger 'AutoSchema' object has no attribute get_link - django

I am trying to browse the api documentation of rest framework swagger. but getting error AttributeError: 'AutoSchema' object has no attribute 'get_link
I have used django rest framework swagger. but not able to browse the documentation.
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('auth.urls')),
path('docs/', schema_view),
]

add below line in settings.py
REST_FRAMEWORK = {'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema' }

djagno-rest-swagger is deprecated and suggests to use drf-yasg

use drf-yasg it is more useful than djagno-rest-swagger

When I add this to my seetings.py, works fine
REST_FRAMEWORK = {
...
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

Related

Django Rest Swagger Not showing up all the APIs

I am working with Django Rest swagger. But it's not showing up all the APIs.
The url paths for /tickets are missing:
Even path to /dj-rest-auth/user/ is missing as well:
Backend/urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
#path('api/v1/', include(api_urlpatterns)),
#path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('api/', include('ticket.urls')),
path('swagger/', schema_view)
]
Tickets/urls.py
from django.urls import path
from ticket import views
urlpatterns = [
path('tickets/', views.ticket_list),
path('tickets/<int:pk>/', views.ticket_detail),
]
My directory structure:
Try with below code:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
.......
.......
}
I solved this by going in settings.py file and set DEBUG=True.

Django DRF Swagger: In urls.py SimpleRouter / DefaultRouter are not auto discovered by swagger docs

Django DRF Swagger docs are not showing the ModelViewSets API endpoints registered as ROUTERS (not urlpattern).
In the example below standard docs (rest_framework.documentation) are showing/documenting this "follow_up" API and swagger docs are not, total skip nothing is showing.
For urlpatterns all is good, and below code for 'this_is_showing' is being nicely documented:
from urls.py file
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import SimpleRouter, DefaultRouter
from rest_framework_swagger.views import get_swagger_view
from . import views
schema_view = get_swagger_view(title=MY APP API')
router = DefaultRouter()
router.register("follow_up", views.FollowUpViewSet)
urlpatterns = [
url(r'^this_is_showing/$', views.SomeView.as_view(), name='view'),
url(r'docs/', include_docs_urls(
title='API Docs', public=True)),
url(r'^swag/', schema_view),
]
What am I missing?
django-rest-swagger==2.2.0,
djangorestframework==3.11.0
EDIT 1
django-rest-swagger Package not maintained anymore!
Moved to drf_yasg: great tool with swagger and reDocs inside.
The DRF docs suggest that if you want to get the auto-generated API list view you need to use the DefaultRouter. I wonder if the SimpleRouter lacks the introspective mechanism (or other hooks) that django-rest-swagger uses to get its information.
https://www.django-rest-framework.org/api-guide/routers/#defaultrouter
EDIT 1
The DRF-swagger docs say that their example uses the DRF example: https://django-rest-swagger.readthedocs.io/en/latest/
The DRF example uses the default router: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py
EDIT 2
I believe you'll also need to include the router somewhere in your URL patterns. If you look here: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py
Not only is the DefaultRouter being used, but the router that's registered is included in the URL patters:
from django.conf.urls import include, url
from rest_framework.routers import DefaultRouter
from snippets import views
# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
# The API URLs are now determined automatically by the router.
# Additionally, we include the login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls))
]

How to use UserViewSet in djoser 2.0.3?

I am new to Djoser and I am struggling to use the functions UserView, UserViewCreate, UserViewDelete to update,create and delete users.
The code I am using currently in url_patterns is:
from django.conf.urls import re_path
from djoser import views as djoser_views
urlpatterns = [
re_path(r'^user/view/$', djoser_views.UserView.as_view(), name='user-view'),
re_path(r'^user/delete/$', djoser_views.UserDeleteView.as_view(), name='user-delete'),
re_path(r'^user/create/$', djoser_views.UserCreateView.as_view(), name='user-create'),
]
When I use UserView, I am getting the error as :
AttributeError: module 'djoser.views' has no attribute 'UserView'
I read the djoser documentation and saw that:
UserCreateView, UserDeleteView, UserView, PasswordResetView,SetPasswordView, PasswordResetConfirmView, SetUsernameView, ActivationView, and
ResendActivationView
These functions have all been removed and replaced by appropriate sub-views within UserViewSet.
I searched but couldn't find any way to use UserViewSet. Is there any way to use UserViewSet in djoser 2.0.3?
you could try
urlpatterns = [
re_path(r'^user/view/$', djoser_views.UserViewSet.retrieve.as_view(), name='user-view'),
re_path(r'^user/delete/$', djoser_views.UserViewSet.destroy.as_view(), name='user-delete'),
re_path(r'^user/create/$', djoser_views.UserViewSet.create.as_view(), name='user-create'),
]
but I would recommend following there documentation and use the lib as documented.

How to fix "error path not found" in django rest framework

I am trying to build an API of my blogging website using Django rest framework, but my URL is not matching.
I am trying Django Rest framework for the first time so I am not quite able to fix this. But I think I mess this up in url_patterns.
Here is my URL code from the main directory(the directory which contains settings.py) .
`
from django.conf.urls import url,include
from django.contrib import admin
from django.urls import path, include
from blog import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'apipost',views.PostViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
]
`
I am trying url http://127.0.0.1:8000/apipost and expect to get value in json format.
You need to add router.urls to your urlpatterns.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
]
urlpatterns += router.urls
Django REST Framework Won't magically register your router in urlpatterns, you have to do it by yourself. You can use urlpatterns += router.urls if you want to add them to the root of your urlpatterns, or url(r'^api/', include((router.urls, 'app_name'))), if you want to set subpath for them.

Hide Django rest framework Routers Api View Page

I am using DjangoRestFramework 3.3.2 for Routing in my django application. I have 6 different folders for 6 apps and 1 main project app. I have include all 6 apps urls into main url file. Following is my main url file.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^vpc/', include('vpc.urls')),
url(r'^dss/', include('dss.urls')),
url(r'^rds/', include('rds.urls')),
url(r'^compute/', include('compute.urls')),
url(r'^iam/', include('iam.urls')),
]
And this is my one of app url file.
from django.conf.urls import url
from rest_framework import routers
import views.instance_views as instance
import views.snapshot_views as snapshot
router = routers.SimpleRouter()
router.register(r'instance', instance.IntanceViewSet, base_name='instance')
router.register(r'snapshot', snapshot.SnapshotViewSet, base_name='snapshot')
urlpatterns = []
urlpatterns += router.urls
Now my problem is when I open urls in browser, I can see whole url hierarchy. Which is not required.
How do I hide these rendered views. I don't want to show any extra information
I was able to hide view using:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
)
}
But I am still getting all urls under 1 app.
{"instance":"http://127.0.0.1:8000/compute/instance/","keypair":"http://127.0.0.1:8000/compute/keypair/","volume":"http://127.0.0.1:8000/compute/volume/","image":"http://127.0.0.1:8000/compute/image/","snapshot":"http://127.0.0.1:8000/compute/snapshot/"}
In your urls.py change the default router to simple router.
router = routers.SimpleRouter()
You should also add the following snippet in your production settings file to only enable JSONRenderer for the API, This will completely disable the web browsable API.
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
)
}
Just to update on the answers given. You do need to specify the SimpleRouter() router, but often the DefaultRouter() router is useful for viewing and debugging whilst in development.
With that in mind, I would advise doing the simple following step:
if settings.DEBUG:
router = DefaultRouter()
else:
router = SimpleRouter()
Then as normal:
from myproject.users.api.viewsets import UserViewSet
router.register(r'users', UserViewSet)
In settings.py DEBUG=False and your REST API wont show.