| 1 |
<style> |
| 2 |
.promo li a { |
| 3 |
float: left; |
| 4 |
width: 130px; |
| 5 |
height: 20px; |
| 6 |
text-align: center; |
| 7 |
margin: 10px 30px; |
| 8 |
padding: 150px 0 0 0; |
| 9 |
background-position: 0 50%; |
| 10 |
background-size: 130px auto; |
| 11 |
background-repeat: no-repeat; |
| 12 |
font-size: 120%; |
| 13 |
color: black; |
| 14 |
} |
| 15 |
.promo li { |
| 16 |
list-style: none; |
| 17 |
} |
| 18 |
</style> |
| 19 |
|
| 20 |
# Django REST framework 3.12 |
| 21 |
|
| 22 |
REST framework 3.12 brings a handful of refinements to the OpenAPI schema |
| 23 |
generation, plus support for Django's new database-agnostic `JSONField`, |
| 24 |
and some improvements to the `SearchFilter` class. |
| 25 |
|
| 26 |
## Grouping operations with tags. |
| 27 |
|
| 28 |
Open API schemas will now automatically include tags, based on the first element |
| 29 |
in the URL path. |
| 30 |
|
| 31 |
For example... |
| 32 |
|
| 33 |
Method | Path | Tags |
| 34 |
--------------------------------|-----------------|------------- |
| 35 |
`GET`, `PUT`, `PATCH`, `DELETE` | `/users/{id}/` | `['users']` |
| 36 |
`GET`, `POST` | `/users/` | `['users']` |
| 37 |
`GET`, `PUT`, `PATCH`, `DELETE` | `/orders/{id}/` | `['orders']` |
| 38 |
`GET`, `POST` | `/orders/` | `['orders']` |
| 39 |
|
| 40 |
The tags used for a particular view may also be overridden... |
| 41 |
|
| 42 |
```python |
| 43 |
class MyOrders(APIView): |
| 44 |
schema = AutoSchema(tags=["users", "orders"]) |
| 45 |
... |
| 46 |
``` |
| 47 |
|
| 48 |
See [the schema documentation](https://www.django-rest-framework.org/api-guide/schemas/#grouping-operations-with-tags) for more information. |
| 49 |
|
| 50 |
## Customizing the operation ID. |
| 51 |
|
| 52 |
REST framework automatically determines operation IDs to use in OpenAPI |
| 53 |
schemas. The latest version provides more control for overriding the behavior |
| 54 |
used to generate the operation IDs. |
| 55 |
|
| 56 |
See [the schema documentation](https://www.django-rest-framework.org/api-guide/schemas/#operationid) for more information. |
| 57 |
|
| 58 |
## Support for OpenAPI components. |
| 59 |
|
| 60 |
In order to output more graceful OpenAPI schemes, REST framework 3.12 now |
| 61 |
defines components in the schema, and then references them inside request |
| 62 |
and response objects. This is in contrast with the previous approach, which |
| 63 |
fully expanded the request and response bodies for each operation. |
| 64 |
|
| 65 |
The names used for a component default to using the serializer class name, [but |
| 66 |
may be overridden if needed](https://www.django-rest-framework.org/api-guide/schemas/#components |
| 67 |
)... |
| 68 |
|
| 69 |
```python |
| 70 |
class MyOrders(APIView): |
| 71 |
schema = AutoSchema(component_name="OrderDetails") |
| 72 |
``` |
| 73 |
|
| 74 |
## More Public API |
| 75 |
|
| 76 |
Many methods on the `AutoSchema` class have now been promoted to public API, |
| 77 |
allowing you to more fully customize the schema generation. The following methods |
| 78 |
are now available for overriding... |
| 79 |
|
| 80 |
* `get_path_parameters` |
| 81 |
* `get_pagination_parameters` |
| 82 |
* `get_filter_parameters` |
| 83 |
* `get_request_body` |
| 84 |
* `get_responses` |
| 85 |
* `get_serializer` |
| 86 |
* `get_paginator` |
| 87 |
* `map_serializer` |
| 88 |
* `map_field` |
| 89 |
* `map_choice_field` |
| 90 |
* `map_field_validators` |
| 91 |
* `allows_filters`. |
| 92 |
|
| 93 |
See [the schema docs](https://www.django-rest-framework.org/api-guide/schemas/#per-view-customization) |
| 94 |
for details on using custom `AutoSchema` subclasses. |
| 95 |
|
| 96 |
## Support for JSONField. |
| 97 |
|
| 98 |
Django 3.1 deprecated the existing `django.contrib.postgres.fields.JSONField` |
| 99 |
in favor of a new database-agnositic `JSONField`. |
| 100 |
|
| 101 |
REST framework 3.12 now supports this new model field, and `ModelSerializer` |
| 102 |
classes will correctly map the model field. |
| 103 |
|
| 104 |
## SearchFilter improvements |
| 105 |
|
| 106 |
There are a couple of significant improvements to the `SearchFilter` class. |
| 107 |
|
| 108 |
### Nested searches against JSONField and HStoreField |
| 109 |
|
| 110 |
The class now supports nested search within `JSONField` and `HStoreField`, using |
| 111 |
the double underscore notation for traversing which element of the field the |
| 112 |
search should apply to. |
| 113 |
|
| 114 |
```python |
| 115 |
class SitesSearchView(generics.ListAPIView): |
| 116 |
""" |
| 117 |
An API view to return a list of archaeological sites, optionally filtered |
| 118 |
by a search against the site name or location. (Location searches are |
| 119 |
matched against the region and country names.) |
| 120 |
""" |
| 121 |
|
| 122 |
queryset = Sites.objects.all() |
| 123 |
serializer_class = SitesSerializer |
| 124 |
filter_backends = [filters.SearchFilter] |
| 125 |
search_fields = ["site_name", "location__region", "location__country"] |
| 126 |
``` |
| 127 |
|
| 128 |
### Searches against annotate fields |
| 129 |
|
| 130 |
Django allows querysets to create additional virtual fields, using the `.annotate` |
| 131 |
method. We now support searching against annotate fields. |
| 132 |
|
| 133 |
```python |
| 134 |
class PublisherSearchView(generics.ListAPIView): |
| 135 |
""" |
| 136 |
Search for publishers, optionally filtering the search against the average |
| 137 |
rating of all their books. |
| 138 |
""" |
| 139 |
|
| 140 |
queryset = Publisher.objects.annotate(avg_rating=Avg("book__rating")) |
| 141 |
serializer_class = PublisherSerializer |
| 142 |
filter_backends = [filters.SearchFilter] |
| 143 |
search_fields = ["avg_rating"] |
| 144 |
``` |
| 145 |
|
| 146 |
--- |
| 147 |
|
| 148 |
## Deprecations |
| 149 |
|
| 150 |
### `serializers.NullBooleanField` |
| 151 |
|
| 152 |
`serializers.NullBooleanField` is now pending deprecation, and will be removed in 3.14. |
| 153 |
|
| 154 |
Instead use `serializers.BooleanField` field and set `allow_null=True` which does the same thing. |
| 155 |
|
| 156 |
--- |
| 157 |
|
| 158 |
## Funding |
| 159 |
|
| 160 |
REST framework is a *collaboratively funded project*. If you use |
| 161 |
REST framework commercially we strongly encourage you to invest in its |
| 162 |
continued development by **[signing up for a paid plan][funding]**. |
| 163 |
|
| 164 |
*Every single sign-up helps us make REST framework long-term financially sustainable.* |
| 165 |
|
| 166 |
*Many thanks to all our [wonderful sponsors][sponsors], and in particular to our premium backers, [Sentry](https://getsentry.com/welcome/), [Stream](https://getstream.io/?utm_source=drf&utm_medium=banner&utm_campaign=drf), [ESG](https://software.esg-usa.com/), [Rollbar](https://rollbar.com/?utm_source=django&utm_medium=sponsorship&utm_campaign=freetrial), [Cadre](https://cadre.com), [Kloudless](https://hubs.ly/H0f30Lf0), [Lights On Software](https://lightsonsoftware.com), and [Retool](https://retool.com/?utm_source=djangorest&utm_medium=sponsorship).* |
| 167 |
|
| 168 |
[sponsors]: https://fund.django-rest-framework.org/topics/funding/#our-sponsors |
| 169 |
[funding]: https://opencollective.com/django-rest-framework |