19đź‘Ť
the namespaces to exclude are the one defined in your urls.py.
So for example, in your case:
urls.py:
internal_apis = patterns('',
url(r'^/api/jobs/status/',...),
url(r'^/api/jobs/parameters/',...),
)
urlpatterns = urlpatterns + patterns('',
url(r'^', include(internal_apis, namespace="internal_apis")),
...
)
and in your settings.py:
SWAGGER_SETTINGS = {
"exclude_namespaces": ["internal_apis"], # List URL namespaces to ignore
}
This is well described in there
18đź‘Ť
For all of those who found the above answer not helpful:
I guess that “exclude_namespaces” doesn’t work anymore in new versions of django swagger. I had almost the same problem (I didnt’t want to show my internal apis in documentation) and the above solution didn’t work for me. I’ve been searching for like an hour for a solution and finally found something helpful.
There are some attributes that you can pass to SchemaGenerator. One of them is urlconf. You can set it to be “yourproject.api.urls” and it will get only urls defined there! Of course, you have to make sure that all the urls that you want to exclude from your api documentation are not included there.
I hope that at least one person found my answer helpful ;).
A problem comes when you want to have many urls.py included in your api documentation. I don’t know what should be done then. If anyone comes up with an answer to this new problem – feel free to comment my answer. thanks!
- How do I get the django HttpRequest from a django rest framework Request?
- Django makemigrations not detecting project/apps/myapp
- How to write unit tests for django-rest-framework api's?
- Email as username in Django
- How to make Django Password Reset Email Beautiful HTML?
8đź‘Ť
With new version of django swagger, we don’t need to create view to exclude some urls. Below code will disable test2 url.
from rest_framework_swagger.views import get_swagger_view
urlpatterns1 = [
url(r'^', include(router.urls)),
url(r'^test/', include('test.urls')),
url(r'^test1/', Test2.as_view()),
]
schema_view = get_swagger_view(title='API Documentation', patterns=urlpatterns1)
urlpatterns = urlpatterns1 + [
url(r'^docs/', schema_view),
url(r'^test2/', Test2.as_view()),
]
- Django: Distinct foreign keys
- Graphene-python performance issues for large data sets
- How do I hide the field label for a HiddenInput widget in Django Admin?
- Django order_by a property
- Logout Django Rest Framework JWT
6đź‘Ť
Ola’s answer is correct. exclude_namespaces
is no longer supported.
For finer control of the documentation, create your own schema view by using a function-based or class-based view. This can be useful if you want to produce documentation for specific URL patterns, or URL confs.
In your views.py
, you can do the following:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework_swagger import renderers
class SwaggerSchemaView(APIView):
renderer_classes = [
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator(title='Your API Documentation', urlconf='your_app.urls')
schema = generator.get_schema(request=request)
return Response(schema)
The above will only render documentation for the URLs that are specified in the urlconf
argument of the SchemaGenerator
. Also, don’t forget to set up your urls.py
as well:
from django.conf.urls import url
from views import SwaggerSchemaView
urlpatterns = [
url(r'^api/v1/docs/$', SwaggerSchemaView.as_view(), name='docs'),
]
- Django admin many-to-many intermediary models using through= and filter_horizontal
- How to make follower-following system with django model
- Subclassing Django ModelForms
- Django Rest Framework debug post and put requests
- How to create custom groups in django from group
4đź‘Ť
For the newest version of drf-swagger you can defile url patterns in the schema generator.
For example:
url_patterns = (
url(r'^api/v1/', include(router.urls, namespace='api')),
)
generator = schemas.SchemaGenerator(title='Core API', patterns=url_patterns)
- Relation does not exist error in Django
- Django – Change a ForeignKey relation to OneToOne
- Determine if an attribute is a `DeferredAttribute` in django
- Django cms – invalid block tag endblock
- Django widget override template
3đź‘Ť
A more flexible solution would be:
from django.contrib import admin
from django.urls import include, path
from rest_framework_swagger.views import get_swagger_view
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls', namespace="user")),
path('locations/', include('location.urls')),
path('departments/', include('department.urls', namespace="department")),
path('my_secret_api/', include('secret.urls', namespace="secret_api")),
]
to_exclude = ['secret_api',] # some more namespaces here
swagger_urls = [item for item in urlpatterns if hasattr(item,"namespace") and item.namespace not in to_exclude]
schema_view = get_swagger_view(title='Highky', patterns=swagger_urls)
urlpatterns += [
path('api/docs/', schema_view),
]
urlpatterns
will have all five paths, but swagger_urls
will have four paths excluding secret_api
.
All of your URLs and includes will continue to work as they were, except we are now passing our modified urlpatterns
that we want to show in the Swagger docs. The checks will also cover the include where you don’t specify a namespace (like in our case, where the namespace is not defined in the location).
- How to convert request.user into a proxy auth.User class?
- Django render_to_string() ignores {% csrf_token %}
- Django prefetch_related's Prefetch, order_by?
- Email as username in Django
1đź‘Ť
views.py
any view class
class ...ViewSet(viewsets.ModelViewSet):
queryset = ....objects.all().order_by('-id')
serializer_class = ...Serializer
http_method_names = ['get', 'post', 'patch', 'delete'] # add or exclude
any function-based view
@api_view(['get']) # target field
def function(request):
...
return Response(...)
- Django REST Framework: Validate before a delete
- Clean Up HTML in Python
- Django collectstatic no such file or directory
- Django – inlineformset_factory with more than one ForeignKey