django router

API versioning with django rest framework?

API versions can affect the demeanor of customers. The formation of REST gives you an opportunity to extend several version control schemes.

Versioning depends on the incoming requests of the customer and can be based on the URL of the request or the request headers.

Exist some valid approaches to approaching versions. Unverified systems can come up, especially if development is required for long-term systems with multiple customers beyond control.

The application should possess a good API with the versions: changes and modern functionality need to be realized in fresh versions of the API, not only in one version. Existing customers can keep on exploiting the old version of the compatible API together with the new one. New or updated customers can exploit the fresh version.

When version control is enabled, the request.version attribute will contain the string corresponding to the version requested in the client’s incoming request. By default, versioning is not enabled, and request.version always returns None.

Transforming the behavior of the API depends on you, but one example is the transition to a different serialization style in a newer version.

The inverse function included in the REST framework is associated with the versioning scheme. You need to include the current query as the keyword argument. When exploiting serialization with a hyperlink, along with the version control system based on the URL, the query must be included as a context in the serializer.

AcceptHeaderVersioning requires that the version be specified as part of the media type in the Accept header. The version is included as a media type parameter that complements the main media type.

NamespaceVersioning is similar to URLPathVersioning. The only difference is how it is configured in the Django application, because it uses URL placement instead of the URL keyword arguments. Using this schema, the request.version attribute is defined based on the namespace that corresponds to the path of the incoming request. In the version control scheme for hosts, the requested version must be specified as part of the hostname in the URL.

Of course, there are pros and cons of using this method. Here are some of them.

The advantages are a simple implementation, the versions of the corresponding classes are classified according to the module base, v1, v2, etc., the ease of navigating the code, there is no need to copy the source code and serializers and less IF-nesting.

Minuses include a deeper level of nesting, when there are a large number of versions of the API. And it’s also harder to debug the code due to nesting.

Igor Grigorenko

Add comment