Article 014
Different Views per API Version in Django REST Framework
Register version-specific DRF views with django-api-versioning, compare route carry-forward with ViewSet routers, and reuse shared behavior through mixins.
Package disclosure: The author is the creator, owner, and maintainer of
django-api-versioning.
When an API evolves, some endpoints need different implementations in different versions. The change may involve more than a serializer: a newer version might introduce additional actions, different query behavior, or a different response structure.
For projects using APIView or DRF generic views, django-api-versioning on PyPI provides declarative registration of version-specific implementations. Each view declares the version from which it should serve, and the package generates the corresponding URL patterns.
The decorator-based @endpoint API manages endpoint-to-version registration for individual views; it does not configure DRF request handling, serializers, or automatically populate DRF's request.version. For resource-oriented ViewSets, the separate VersionedRouter integration delegates route generation to DRF's SimpleRouter, and VersionedRouterVersioning exposes the matched namespace through request.version.
Register different views for different API versions
Consider an API with two versions. Both versions expose user-list and user-detail endpoints, but v2 changes their serializers and adds a profile endpoint.
The examples assume:
Useris the project's configured user model.UserSerializerV1andUserSerializerV2are existing serializers.Userhas a reverse one-to-oneProfilerelation accessible asuser.profile.Userhas a many-to-many or reverse relation namedfollowers, used by the mixin example.- Every user returned by the profile endpoint has an associated profile. Otherwise, access to
user.profile.biomust handle the missing related object.
Configure the supported version range:
# settings.py
API_MIN_VERSION = 1
API_MAX_VERSION = 2Declare the v1 views:
# accounts/views.py
from django.contrib.auth import get_user_model
from django.shortcuts import get_object_or_404
from django_api_versioning import endpoint
from rest_framework.generics import (
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
)
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import UserSerializerV1, UserSerializerV2
User = get_user_model()
@endpoint("users/", version=1)
class UserListViewV1(ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1
@endpoint("users/<int:pk>/", version=1)
class UserDetailViewV1(RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializerV1Now declare the v2 implementations:
# accounts/views.py
@endpoint("users/", version=2)
class UserListViewV2(UserListViewV1):
serializer_class = UserSerializerV2
@endpoint("users/<int:pk>/", version=2)
class UserDetailViewV2(UserDetailViewV1):
serializer_class = UserSerializerV2
@endpoint("users/<int:pk>/profile/", version=2)
class UserProfileViewV2(APIView):
def get(self, request, pk):
user = get_object_or_404(User, pk=pk)
return Response({
"bio": user.profile.bio,
})Here, Python class inheritance and route-registration inheritance are separate mechanisms:
- Python inheritance reuses the implementation of the v1 generic views.
- The
@endpointdeclarations determine which implementation is registered for each API version.
The v2 classes inherit the generic-view behavior but replace the serializer. The profile endpoint is a new v2-only route.
Mount the generated URL patterns
Import modules containing endpoint declarations during URLconf initialization so their decorators register routes before Django first resolves requests:
# project/urls.py
from django.urls import include, path
from accounts import views as account_views # noqa: F401
from django_api_versioning.urls import urlpatterns as api_urlpatterns
urlpatterns = [
path("", include(api_urlpatterns)),
]The account_views import executes the decorators and populates the registry. The package does not automatically discover and import application view modules. Its exported urlpatterns references the registry's shared list rather than a copy, so importing that list before the view module does not permanently capture an empty snapshot. Keep imports explicit and ensure registration is complete before request resolution.
With the default API_BASE_PATH = "api/v{version}/", the declarations produce these routes:
| URL pattern | Registered view | Registration |
|---|---|---|
/api/v1/users/ | UserListViewV1 | v1 |
/api/v2/users/ | UserListViewV2 | v2 replaces the v1 registration for v2 |
/api/v1/users/<int:pk>/ | UserDetailViewV1 | v1 |
/api/v2/users/<int:pk>/ | UserDetailViewV2 | v2 replaces the v1 registration for v2 |
/api/v2/users/<int:pk>/profile/ | UserProfileViewV2 | v2 |
There are five declarations and five final URL patterns: two routes in v1 and three in v2.
Understand route carry-forward and replacement
The package supports two version-registration modes:
version=Nregisters the implementation from version N throughAPI_MAX_VERSION, inclusive.versions=[...]registers the implementation only for the explicitly listed versions.
For example, version=1 initially registers an endpoint for both v1 and v2 when the configured maximum is 2. A later declaration for the same route with version=2 replaces the v2 registration.
This is registration-time behavior. The package does not search older view classes when a request arrives, and it does not create Python inheritance between them.
Now:
/api/v1/users/resolves toUserListViewV1./api/v2/users/resolves toUserListViewV2./api/v2/users/<int:pk>/profile/resolves toUserProfileViewV2.- The profile endpoint has no v1 registration.
Scope: routing only. The package does not decide whether a response change is backward compatible, merge serializers, or share business logic between versions.
For the exact behavior of version validation, route construction, collision precedence, and import behavior, see the canonical versioning contract.
When the project uses ModelViewSet
The preceding example uses DRF generic views. This is an important boundary: @endpoint does not directly support decorating a ModelViewSet class. The package does, however, provide the separate VersionedRouter integration for full router-managed ViewSet routes.
A ViewSet requires an action mapping when converted into a Django view callable. The package's class-based view handling calls as_view() without that mapping, so decorating a ViewSet class directly does not work.
For resource-oriented endpoints that rely on ModelViewSet, use VersionedRouter when you want the package's route-level inheritance and version-aware reversal. Plain DRF SimpleRouter instances mounted separately for each version remain a valid alternative when you prefer to manage version prefixes and route registrations yourself.
For example, each API version can have its own ViewSet:
# accounts/views.py
from django.contrib.auth import get_user_model
from rest_framework import viewsets
from .serializers import UserSerializerV1, UserSerializerV2
User = get_user_model()
class UserViewSetV1(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializerV1
class UserViewSetV2(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializerV2Register them with separate routers:
# accounts/urls.py
from django.urls import include, path
from rest_framework.routers import SimpleRouter
from .views import UserViewSetV1, UserViewSetV2
v1_router = SimpleRouter()
v1_router.register("users", UserViewSetV1, basename="v1-user")
v2_router = SimpleRouter()
v2_router.register("users", UserViewSetV2, basename="v2-user")
urlpatterns = [
path("api/v1/", include(v1_router.urls)),
path("api/v2/", include(v2_router.urls)),
]This is standard DRF-only router configuration. The router generates the list, detail, and applicable action routes for each ViewSet, but the application must maintain each version's registrations and URL prefixes.
When you want the package registry to manage version-specific ViewSet routes, use VersionedRouter instead:
# accounts/urls.py
from django_api_versioning.routers import VersionedRouter
from .views import UserViewSetV1, UserViewSetV2
router = VersionedRouter()
router.register("users", UserViewSetV1, version=1, basename="user")
router.register("users", UserViewSetV2, version=2, basename="user")
urlpatterns = router.urlsConfigure VersionedRouterVersioning as DRF's DEFAULT_VERSIONING_CLASS (or on the selected ViewSets) when the views need request.version and version-aware reversal. See Versioned DRF ViewSets with Route-Level Inheritance for the complete contract.
A configured ViewSet callable is a narrower option
A configured as_view() callable can be registered with @endpoint for one specific route:
# accounts/versioned_routes.py
from django_api_versioning import endpoint
from .views import UserViewSetV1
user_list_v1 = endpoint("users/", version=1)(
UserViewSetV1.as_view({
"get": "list",
"post": "create",
})
)This registers the explicitly configured list/create callable. It does not generate the ViewSet's detail route or its other actions. Those still require their own configured callables or normal router registration.
Do not mount the same route through both this callable and a router unless the duplicate registration is intentional and its precedence is understood.
Share behavior with mixins when versions overlap
Separate ViewSet classes do not require duplicating all their behavior. When several versions share CRUD operations, DRF's generic mixins can provide a common base.
For example:
# accounts/mixins.py
from django.db.models import Count
from rest_framework.decorators import action
from rest_framework.response import Response
class UserV2ExtendedMixin:
@action(detail=True, methods=["get"])
def profile_extended(self, request, pk=None):
user = self.get_object()
return Response({
"bio": user.profile.bio,
})
@action(detail=False, methods=["get"])
def trending(self, request):
users = (
self.get_queryset()
.annotate(follower_count=Count("followers"))
.order_by("-follower_count")[:10]
)
serializer = self.get_serializer(users, many=True)
return Response(serializer.data)Compose the shared CRUD behavior and the v2-specific actions:
# accounts/views.py
from django.contrib.auth import get_user_model
from rest_framework import mixins, viewsets
from .mixins import UserV2ExtendedMixin
from .serializers import UserSerializerV1, UserSerializerV2
User = get_user_model()
class BaseUserViewSet(
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
queryset = User.objects.all()
class UserViewSetV1(BaseUserViewSet):
serializer_class = UserSerializerV1
class UserViewSetV2(UserV2ExtendedMixin, BaseUserViewSet):
serializer_class = UserSerializerV2The routers still need to register each version's ViewSet. The mixins reduce duplicated view behavior; they do not replace URL registration.
DRF routers discover the extra actions declared on the ViewSet, including inherited methods decorated with @action. Keep mixins cohesive and avoid building a deep composition hierarchy that makes the effective behavior difficult to trace.
Compare the registration responsibilities
The approaches solve related but different problems:
| Concern | django-api-versioning with generic views | Per-version ViewSets and routers | Mixin-based ViewSets | VersionedRouter |
| --- | --- | --- | --- | --- | --- |
| Main registration mechanism | @endpoint declarations | SimpleRouter registrations | SimpleRouter registrations | VersionedRouter registrations |
| Version-specific implementations | Separate decorated views | Separate ViewSet classes | Separate ViewSet classes | Separate ViewSet classes |
| Shared behavior | Python inheritance or shared helpers | Optional Python inheritance | Composed mixins and shared base classes | Optional Python inheritance or mixins |
| Carry-forward of unchanged routes | Supported through version=N | Must be represented in router configuration | Must be represented in router configuration | Supported per generated route through the package registry |
| ModelViewSet and @action | Not supported by direct class decoration; use a configured callable only for one route | Supported | Supported | Supported through DRF SimpleRouter integration |
| URLconf structure | One include for generated package routes | Router URLs mounted under each version | Router URLs mounted under each version | VersionedRouter-generated version namespaces |
| Import requirement | Import decorated modules during URLconf initialization; exported urlpatterns is a shared list | Import ViewSets before router registration | Import ViewSets and mixins before router registration | Import ViewSets and configure the versioning adapter before requests |
The package is most useful when the main maintenance problem is registering endpoint implementations across versions, particularly when many endpoints remain unchanged and only selected implementations need replacement.
Plain per-version routers are appropriate when the team wants to manage each version's URL configuration directly. VersionedRouter is the package-integrated option when the API uses resource-oriented ViewSets and benefits from per-route carry-forward and version-aware reversal.
Trade-offs and scope
Declarative registration reduces the need to repeat version-specific URL configuration. It also keeps the route's version declaration next to the view implementation.
However, segment registration has an important consequence: a declaration such as version=1 remains registered through API_MAX_VERSION unless a later declaration replaces it.
If an endpoint's contract changes but its registration does not, the earlier implementation can continue serving later versions. Teams should review the route matrix whenever they change API contracts and test the behavior of each supported version.
The package does not:
- Create Python inheritance between view classes.
- Merge serializers, permissions, or business logic.
- Determine whether a response change is backward compatible.
- Allow
@endpointto decorateModelViewSetclasses directly. - Discover arbitrary
SimpleRouterorDefaultRouterinstances; useVersionedRouterfor the supportedSimpleRouterintegration. - Generate or maintain version-specific OpenAPI schemas.
For projects that only need a small number of version-specific ViewSets, per-version routers may be sufficient. For projects that use generic views and need a declarative mapping between endpoint implementations and versions, the package offers a different registration model.
The choice should follow the project's endpoint structure and the maintenance burden of its versioned routing—not an arbitrary threshold based on the number of versions. The package's exported URL pattern list is shared with the registry; importing it before decorated view modules does not freeze an empty snapshot, but all declarations must be registered before request resolution.
Conclusion
Different API versions can use separate view implementations without requiring every endpoint to be implemented from scratch.
For generic views, django-api-versioning provides declarative endpoint registration and explicit carry-forward and replacement behavior. For resource-oriented ViewSets, choose plain DRF routers for fully manual per-version routing or VersionedRouter for package-managed route inheritance. Mixins remain useful when ViewSet versions share implementation behavior.
The important distinction is between selecting an implementation for a versioned route and composing the Python behavior of that implementation. Those responsibilities can be handled independently.
Next step: review the django-api-versioning repository and compare its generated route matrix with the version-specific endpoints in your project.