django-rest-framework/docs/community/3.11-announcement.md
J.V. Zammit e794e5e5e4
blacken-docs (#8906)
* blacken-docs: Manual fixes for command to pass without errors

* blacken-docs: Adds blacken-docs step to precommit hook.

* blacken-docs: Adds changes made by command itself.

* blacken-docs: Modifies blacken-docs step to only process files under "docs" directory

* blacken-docs: Updates pre-commit config file to exclude all directories other than "docs" to be compatible with "--all-files" flag.

* blacken-docs: Adds commas at the end to make it look identical to pre-black version
2023-10-13 12:44:45 +01:00

5.3 KiB

Django REST framework 3.11

The 3.11 release adds support for Django 3.0.

  • Our supported Python versions are now: 3.5, 3.6, 3.7, and 3.8.
  • Our supported Django versions are now: 1.11, 2.0, 2.1, 2.2, and 3.0.

This release will be the last to support Python 3.5 or Django 1.11.

OpenAPI Schema Generation Improvements

The OpenAPI schema generation continues to mature. Some highlights in 3.11 include:

  • Automatic mapping of Django REST Framework renderers and parsers into OpenAPI request and response media-types.
  • Improved mapping JSON schema mapping types, for example in HStoreFields, and with large integer values.
  • Porting of the old CoreAPI parsing of docstrings to form OpenAPI operation descriptions.

In this example view operation descriptions for the get and post methods will be extracted from the class docstring:

class DocStringExampleListView(APIView):
    """
    get: A description of my GET operation.
    post: A description of my POST operation.
    """

    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    def get(self, request, *args, **kwargs):
        ...

    def post(self, request, *args, **kwargs):
        ...

Validator / Default Context

In some circumstances a Validator class or a Default class may need to access the serializer field with which it is called, or the .context with which the serializer was instantiated. In particular:

  • Uniqueness validators need to be able to determine the name of the field to which they are applied, in order to run an appropriate database query.
  • The CurrentUserDefault needs to be able to determine the context with which the serializer was instantiated, in order to return the current user instance.

Previous our approach to this was that implementations could include a set_context method, which would be called prior to validation. However this approach had issues with potential race conditions. We have now move this approach into a pending deprecation state. It will continue to function, but will be escalated to a deprecated state in 3.12, and removed entirely in 3.13.

Instead, validators or defaults which require the serializer context, should include a requires_context = True attribute on the class.

The __call__ method should then include an additional serializer_field argument.

Validator implementations will look like this:

class CustomValidator:
    requires_context = True

    def __call__(self, value, serializer_field):
        ...

Default implementations will look like this:

class CustomDefault:
    requires_context = True

    def __call__(self, serializer_field):
        ...

Funding

REST framework is a collaboratively funded project. If you use REST framework commercially we strongly encourage you to invest in its continued development by signing up for a paid plan.

Every single sign-up helps us make REST framework long-term financially sustainable.

Many thanks to all our wonderful sponsors, and in particular to our premium backers, Sentry, Stream, ESG, Rollbar, Cadre, Kloudless, Lights On Software, and Retool.