File: django.md

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (131 lines) | stat: -rw-r--r-- 3,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Django

This section describes the integration with the [Django](https://www.djangoproject.com) web framework.
The integration supports Django version 3.0 and above.

## Middleware

Django can be integrated using [middleware](https://docs.djangoproject.com/en/5.0/topics/http/middleware/) to apply OpenAPI validation to your entire application.

Add `DjangoOpenAPIMiddleware` to your `MIDDLEWARE` list and define `OPENAPI`.

``` python hl_lines="5 8" title="settings.py"
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.

``` python
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
```

### Response validation

You can skip the response validation process by setting `OPENAPI_RESPONSE_CLS` to `None`.

``` python hl_lines="9" title="settings.py"
from openapi_core import OpenAPI

MIDDLEWARE = [
    # ...
    'openapi_core.contrib.django.middlewares.DjangoOpenAPIMiddleware',
]

OPENAPI = OpenAPI.from_dict(spec_dict)
OPENAPI_RESPONSE_CLS = None
```

## Decorator

Django can be integrated using [view decorators](https://docs.djangoproject.com/en/5.1/topics/http/decorators/) to apply OpenAPI validation to your application's specific views.

Use `DjangoOpenAPIViewDecorator` with the OpenAPI object to create the decorator.

``` python hl_lines="1 3 6"
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`.

``` python hl_lines="5"
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:

``` python hl_lines="3"
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"
```

## Low level

The integration defines classes useful for low-level integration.

### Request

Use `DjangoOpenAPIRequest` to create an OpenAPI request from a Django request:

``` python
from openapi_core.contrib.django import DjangoOpenAPIRequest

class MyView(View):
    def get(self, request):
        openapi_request = DjangoOpenAPIRequest(request)
        openapi.validate_request(openapi_request)
```

### Response

Use `DjangoOpenAPIResponse` to create an OpenAPI response from a Django response:

``` python
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
```