How to control versioning in Django Rest Framework (DRF)

Viewed 1421

I want to know what is the best practices for controlling the Version for Mobile App APIs.

Requirement

  • If I change something in my database previous version of the app should not be affected.
  • Currently I'm doing like...
path('v1/auth/', include('authentication.urls')),
path('v2/auth/', include('authentication.urls2')), # Example
path('v1/api/', include('contentstudio.urls')),
2 Answers

Django REST Framework has support for many different methods of versioning you api. Check out the docs to find which one suits you best.

According to the docs, the AcceptHeaderVersioning method "is generally considered as best practice". ie. you put the version in the Accept header like so:

Accept: application/json; version=1.0

Using any of these methods you will have access to request.version in your views to determine the behavior of the different versions.

AcceptHeaderVersioning is the best way but when you have many versions it may seem complex. I choose namespace because it's easy to implement. You can see an exmaple here: sample from github

Also you can add settings docs to settings.py for better configuration of versioning

Related