This section describes the integration with the Django web framework. The integration supports Django version 3.0 and above.
Django can be integrated using middleware to apply OpenAPI validation to your entire application.
Add DjangoOpenAPIMiddleware
to your MIDDLEWARE
list and define OPENAPI
.
from openapi_core import OpenAPI
MIDDLEWARE = [
# ...
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]
OPENAPI = OpenAPI.from_dict(spec_dict)
After that, all your requests and responses will be validated.
You also have access to the unmarshalled result object with all unmarshalled request data through the openapi
attribute of the request object.
from django.views import View
class MyView(View):
def get(self, request):
# Get parameters object with path, query, cookies, and headers parameters
unmarshalled_params = request.openapi.parameters
# Or specific location parameters
unmarshalled_path_params = request.openapi.parameters.path
# Get body
unmarshalled_body = request.openapi.body
# Get security data
unmarshalled_security = request.openapi.security
You can skip the response validation process by setting OPENAPI_RESPONSE_CLS
to None
.
from openapi_core import OpenAPI
MIDDLEWARE = [
# ...
'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]
OPENAPI = OpenAPI.from_dict(spec_dict)
OPENAPI_RESPONSE_CLS = None
Django can be integrated using view decorators to apply OpenAPI validation to your application's specific views.
Use DjangoOpenAPIViewDecorator
with the OpenAPI object to create the decorator.
from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator
openapi_validated = DjangoOpenAPIViewDecorator(openapi)
@openapi_validated
def home():
return "Welcome home"
You can skip the response validation process by setting response_cls
to None
.
from openapi_core.contrib.django.decorators import DjangoOpenAPIViewDecorator
openapi_validated = DjangoOpenAPIViewDecorator(
openapi,
response_cls=None,
)
If you want to decorate a class-based view, you can use the method_decorator
decorator:
from django.utils.decorators import method_decorator
@method_decorator(openapi_validated, name='dispatch')
class MyView(View):
def get(self, request, *args, **kwargs):
return "Welcome home"
The integration defines classes useful for low-level integration.
Use DjangoOpenAPIRequest
to create an OpenAPI request from a Django request:
from openapi_core.contrib.django import DjangoOpenAPIRequest
class MyView(View):
def get(self, request):
openapi_request = DjangoOpenAPIRequest(request)
openapi.validate_request(openapi_request)
Use DjangoOpenAPIResponse
to create an OpenAPI response from a Django response:
from openapi_core.contrib.django import DjangoOpenAPIResponse
class MyView(View):
def get(self, request):
response = JsonResponse({'hello': 'world'})
openapi_request = DjangoOpenAPIRequest(request)
openapi_response = DjangoOpenAPIResponse(response)
openapi.validate_response(openapi_request, openapi_response)
return response