1

I'm working on an API, using django rest framework which is currently in production. The versioning strategy in use is namespace versioning. I would like to switch to acceptheader versioning as it seems to suit the project better.

Is there a way to make the change smoothly without breaking previous API versions.

Timothy Oliver
  • 470
  • 9
  • 19

1 Answers1

1

Versioning is very simple.

Create folder in your app and name it to v1

Like this image:

enter image description here

yourApp > urls.py should be like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('v1/', include('api.v1.urls')),
]

Then create urls.py in v1 folder, it should something like this:

from django.urls import path, include
from api.v1.classes.Plan import listPlan
from api.v1.classes.preInvoce import preInvoce    

urlpatterns = [
    path('plan/list', listPlan.as_view(), name="listPlans"),
    path('plan/buy', preInvoce.as_view(), name="preInvoice"),
]

If you want have version 2 you need to create new folder called v2 and have urls.py in it.

and your app urls.py should be like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('v1/', include('api.v1.urls')),
    path('v2/', include('api.v2.urls')),
]

Put your own and new urls in v2/urls.py

Your final v1 urls like this:

localhost:8000/v1/planList

Your final v2 urls like this:

localhost:8000/v2/newUrlInV2

Both v1 and v2 urls should works correctly.

smac89
  • 39,374
  • 15
  • 132
  • 179
R.A.M
  • 82
  • 1
  • 8