├── .gitignore ├── LICENSE ├── README.md ├── alpha ├── __init__.py ├── api_v1 │ ├── __init__.py │ ├── serializers.py │ ├── urls.py │ └── views.py ├── api_v2 │ ├── __init__.py │ ├── serializers.py │ ├── urls.py │ └── views.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_alpha_version2.py │ └── __init__.py └── models.py ├── beta ├── __init__.py ├── api │ ├── __init__.py │ ├── urls.py │ ├── v1 │ │ ├── __init__.py │ │ └── serializers.py │ ├── v2 │ │ ├── __init__.py │ │ └── serializers.py │ └── views.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_beta_version2.py │ └── __init__.py ├── mixins.py └── models.py ├── drf_versioning ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ├── manage.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | .idea/ 107 | .vscode 108 | 109 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Vadim Sentyaev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # drf-versioning 2 | Example app with two approaches for versioning API with django rest framework 3 | 4 | First approach implemented in `alpha` django application. 5 | With this approach api v1 and v2 shares model, but has it's own views and serializers. 6 | 7 | Second approach implemented in `beta` django application. 8 | With this approach api v1 and v2 shares model and view, but has it's own serializers. 9 | To share view for both versions `SerializerClassMixin` is introduced. 10 | 11 | It's possible to mix both approaches even in same djanogo application. 12 | 13 | ### Composing urls in main urls.py 14 | 15 | Each application exposes `router` which is drf SimpleRouter 16 | 17 | from rest_framework.routers import SimpleRouter 18 | router = SimpleRouter() 19 | router.register(r'app', AppViewSet) 20 | 21 | Main urls.py instantiate DefaultRouter and extend it with application routers 22 | 23 | from rest_framework.routers import DefaultRouter 24 | from alpha.api_v1.urls import router as alpha_v1_router 25 | from alpha.api_v2.urls import router as alpha_v2_router 26 | from beta.api.urls imprt router as beta_router 27 | 28 | v1_router = DefaultRouter() 29 | v1_router.registry.extend(alpha_v1_router.registry) 30 | v1_router.registry.extend(beta_router) 31 | 32 | v2_router = DefaultRouter() 33 | v2_router.registry.extend(alpha_v2_router.registry) 34 | v2_router.registry.extend(beta_router.registry) 35 | 36 | urlpatterns = [ 37 | path('api/v1/', include((v1_router.urls, 'api'), namespace='v1')), 38 | path('api/v2/', include((v2_router.urls, 'api'), namespace='v2')), 39 | ] 40 | 41 | ### Alpha - completely separated views and serializers for each api version 42 | Application structure 43 | 44 | ├── api_v1 45 | │   ├── serializers.py 46 | │   ├── urls.py 47 | │   └── views.py 48 | ├── api_v2 49 | │   ├── serializers.py 50 | │   ├── urls.py 51 | │   └── views.py 52 | ├── apps.py 53 | └── models.py 54 | 55 | The reason for copy and paste views, serializers and urls is to be able to change anything in specific version and do not affect another one. 56 | 57 | ### Beta - using same view set with different serializers 58 | Application structure 59 | 60 | ├── api 61 | │   ├── v1 62 | │   │   └── serializers.py 63 | │   ├── v2 64 | │   │   └── serializers.py 65 | │   ├── urls.py 66 | │   └── views.py 67 | ├── apps.py 68 | ├── mixins.py 69 | └── models.py 70 | 71 | In this case `SerializerClassMixin` is introduced, so we can define view this way: 72 | 73 | version_map = { 74 | 'v1': BetaSerializer_v1, 75 | 'v2': BetaSerializer_v2, 76 | } 77 | 78 | class BetaViewSet(SerializerClassMixin, viewsets.ModelViewSet): 79 | version_map = version_map 80 | queryset = Beta.objects.all() 81 | 82 | Key for the `version_map` should be same we have in main `urls.py` namespace for specific path. -------------------------------------------------------------------------------- /alpha/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/alpha/__init__.py -------------------------------------------------------------------------------- /alpha/api_v1/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/alpha/api_v1/__init__.py -------------------------------------------------------------------------------- /alpha/api_v1/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from ..models import Alpha 4 | 5 | 6 | class AlphaSerializer(serializers.HyperlinkedModelSerializer): 7 | 8 | def create(self, validated_data): 9 | validated_data['version2'] = 1 10 | return super().create(validated_data) 11 | 12 | class Meta: 13 | model = Alpha 14 | fields = ('url', 'version1') 15 | -------------------------------------------------------------------------------- /alpha/api_v1/urls.py: -------------------------------------------------------------------------------- 1 | from rest_framework.routers import SimpleRouter 2 | 3 | from .views import AlphaViewSet 4 | 5 | router = SimpleRouter() 6 | router.register(r'alpha', AlphaViewSet) 7 | -------------------------------------------------------------------------------- /alpha/api_v1/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import viewsets 2 | 3 | from .serializers import AlphaSerializer 4 | from ..models import Alpha 5 | 6 | 7 | class AlphaViewSet(viewsets.ModelViewSet): 8 | queryset = Alpha.objects.all() 9 | serializer_class = AlphaSerializer 10 | -------------------------------------------------------------------------------- /alpha/api_v2/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/alpha/api_v2/__init__.py -------------------------------------------------------------------------------- /alpha/api_v2/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from ..models import Alpha 4 | 5 | 6 | class AlphaSerializer(serializers.HyperlinkedModelSerializer): 7 | class Meta: 8 | model = Alpha 9 | fields = ('url', 'version1', 'version2') 10 | -------------------------------------------------------------------------------- /alpha/api_v2/urls.py: -------------------------------------------------------------------------------- 1 | from rest_framework.routers import SimpleRouter 2 | 3 | from .views import AlphaViewSet 4 | 5 | router = SimpleRouter() 6 | router.register(r'alpha', AlphaViewSet) 7 | -------------------------------------------------------------------------------- /alpha/api_v2/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import viewsets 2 | 3 | from .serializers import AlphaSerializer 4 | from ..models import Alpha 5 | 6 | 7 | class AlphaViewSet(viewsets.ModelViewSet): 8 | queryset = Alpha.objects.all() 9 | serializer_class = AlphaSerializer 10 | -------------------------------------------------------------------------------- /alpha/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class AlphaConfig(AppConfig): 5 | name = 'alpha' 6 | -------------------------------------------------------------------------------- /alpha/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.4 on 2020-03-18 15:25 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Alpha', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('version1', models.CharField(max_length=10)), 19 | ], 20 | ), 21 | ] 22 | -------------------------------------------------------------------------------- /alpha/migrations/0002_alpha_version2.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.4 on 2020-03-18 15:25 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('alpha', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='alpha', 15 | name='version2', 16 | field=models.PositiveIntegerField(default=2), 17 | preserve_default=False, 18 | ), 19 | ] 20 | -------------------------------------------------------------------------------- /alpha/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/alpha/migrations/__init__.py -------------------------------------------------------------------------------- /alpha/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Alpha(models.Model): 5 | version1 = models.CharField(max_length=10) 6 | version2 = models.PositiveIntegerField() 7 | -------------------------------------------------------------------------------- /beta/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/beta/__init__.py -------------------------------------------------------------------------------- /beta/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/beta/api/__init__.py -------------------------------------------------------------------------------- /beta/api/urls.py: -------------------------------------------------------------------------------- 1 | from rest_framework.routers import SimpleRouter 2 | 3 | from .views import BetaViewSet 4 | 5 | router = SimpleRouter() 6 | router.register(r'beta', BetaViewSet) 7 | 8 | urlpatterns = router.urls 9 | -------------------------------------------------------------------------------- /beta/api/v1/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/beta/api/v1/__init__.py -------------------------------------------------------------------------------- /beta/api/v1/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from beta.models import Beta 4 | 5 | 6 | class BetaSerializer(serializers.HyperlinkedModelSerializer): 7 | 8 | def create(self, validated_data): 9 | validated_data['version2'] = 1 10 | return super().create(validated_data) 11 | 12 | class Meta: 13 | model = Beta 14 | fields = ('url', 'version1') 15 | -------------------------------------------------------------------------------- /beta/api/v2/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/beta/api/v2/__init__.py -------------------------------------------------------------------------------- /beta/api/v2/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from beta.models import Beta 4 | 5 | 6 | class BetaSerializer(serializers.HyperlinkedModelSerializer): 7 | class Meta: 8 | model = Beta 9 | fields = ('url', 'version1', 'version2') 10 | -------------------------------------------------------------------------------- /beta/api/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import viewsets 2 | 3 | from .v1.serializers import BetaSerializer as BetaSerializer_v1 4 | from .v2.serializers import BetaSerializer as BetaSerializer_v2 5 | 6 | from ..models import Beta 7 | from ..mixins import SerializerClassMixin 8 | 9 | 10 | version_map = { 11 | 'v1': BetaSerializer_v1, 12 | 'v2': BetaSerializer_v2, 13 | } 14 | 15 | 16 | class BetaViewSet(SerializerClassMixin, viewsets.ModelViewSet): 17 | version_map = version_map 18 | queryset = Beta.objects.all() 19 | -------------------------------------------------------------------------------- /beta/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class BetaAppConfig(AppConfig): 5 | name = 'beta' 6 | -------------------------------------------------------------------------------- /beta/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.4 on 2020-03-18 15:25 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Beta', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('version1', models.CharField(max_length=10)), 19 | ], 20 | ), 21 | ] 22 | -------------------------------------------------------------------------------- /beta/migrations/0002_beta_version2.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.0.4 on 2020-03-18 15:25 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('beta', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='beta', 15 | name='version2', 16 | field=models.PositiveIntegerField(default=2), 17 | preserve_default=False, 18 | ), 19 | ] 20 | -------------------------------------------------------------------------------- /beta/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/beta/migrations/__init__.py -------------------------------------------------------------------------------- /beta/mixins.py: -------------------------------------------------------------------------------- 1 | class SerializerClassMixin: 2 | version_map = None 3 | 4 | def _get_serializer_class(self, version): 5 | if version not in self.version_map: 6 | raise Exception(f'Version Map does not have Serializer for {version}') 7 | return self.version_map[version] 8 | 9 | def get_serializer_class(self): 10 | if not self.version_map: 11 | raise Exception(f'Version Map not provided for {self.__class__.__name__}') 12 | 13 | version = self.request.version 14 | return self._get_serializer_class(version) 15 | -------------------------------------------------------------------------------- /beta/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Beta(models.Model): 5 | version1 = models.CharField(max_length=10) 6 | version2 = models.PositiveIntegerField() 7 | -------------------------------------------------------------------------------- /drf_versioning/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sentyaev/drf-versioning/40caa5d3c1b40f0eb945a813bb9eecdd4640bf0e/drf_versioning/__init__.py -------------------------------------------------------------------------------- /drf_versioning/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for drf_versioning project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2.4. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '7n4jp567x+9y6u5+mq@bv(p!-*dd41s3zo_e)(&-si*1qb*v$+' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'django_extensions', 42 | 'alpha', 43 | 'beta', 44 | ] 45 | 46 | MIDDLEWARE = [ 47 | 'django.middleware.security.SecurityMiddleware', 48 | 'django.contrib.sessions.middleware.SessionMiddleware', 49 | 'django.middleware.common.CommonMiddleware', 50 | 'django.middleware.csrf.CsrfViewMiddleware', 51 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 52 | 'django.contrib.messages.middleware.MessageMiddleware', 53 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 54 | 55 | 56 | ] 57 | 58 | ROOT_URLCONF = 'drf_versioning.urls' 59 | 60 | TEMPLATES = [ 61 | { 62 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 63 | 'DIRS': [os.path.join(BASE_DIR, 'templates')] 64 | , 65 | 'APP_DIRS': True, 66 | 'OPTIONS': { 67 | 'context_processors': [ 68 | 'django.template.context_processors.debug', 69 | 'django.template.context_processors.request', 70 | 'django.contrib.auth.context_processors.auth', 71 | 'django.contrib.messages.context_processors.messages', 72 | ], 73 | }, 74 | }, 75 | ] 76 | 77 | WSGI_APPLICATION = 'drf_versioning.wsgi.application' 78 | 79 | 80 | # Database 81 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 82 | 83 | DATABASES = { 84 | 'default': { 85 | 'ENGINE': 'django.db.backends.sqlite3', 86 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 87 | } 88 | } 89 | 90 | 91 | # Password validation 92 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 93 | 94 | AUTH_PASSWORD_VALIDATORS = [ 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 100 | }, 101 | { 102 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 103 | }, 104 | { 105 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 106 | }, 107 | ] 108 | 109 | 110 | # Internationalization 111 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 112 | 113 | LANGUAGE_CODE = 'en-us' 114 | 115 | TIME_ZONE = 'UTC' 116 | 117 | USE_I18N = True 118 | 119 | USE_L10N = True 120 | 121 | USE_TZ = True 122 | 123 | 124 | # Static files (CSS, JavaScript, Images) 125 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 126 | 127 | STATIC_URL = '/static/' 128 | 129 | 130 | # DRF Settings 131 | REST_FRAMEWORK = { 132 | # versioning 133 | 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning', 134 | 135 | # pagination 136 | 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 137 | 'PAGE_SIZE': 100, 138 | } 139 | -------------------------------------------------------------------------------- /drf_versioning/urls.py: -------------------------------------------------------------------------------- 1 | """drf_versioning URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | 19 | from django.urls import include, path 20 | 21 | from alpha.api_v1.urls import router as alpha_router_v1 22 | from alpha.api_v2.urls import router as alpha_router_v2 23 | 24 | 25 | from beta.api.urls import router as beta_router 26 | from rest_framework.routers import DefaultRouter 27 | 28 | v1_router = DefaultRouter() 29 | v1_router.registry.extend(alpha_router_v1.registry) 30 | v1_router.registry.extend(beta_router.registry) 31 | 32 | v2_router = DefaultRouter() 33 | v2_router.registry.extend(alpha_router_v2.registry) 34 | v2_router.registry.extend(beta_router.registry) 35 | 36 | urlpatterns = [ 37 | path('api/v1/', include((v1_router.urls, 'api'), namespace='v1')), 38 | path('api/v2/', include((v2_router.urls, 'api'), namespace='v2')), 39 | 40 | 41 | path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) 42 | ] 43 | -------------------------------------------------------------------------------- /drf_versioning/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for drf_versioning project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_versioning.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_versioning.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.2.5 2 | Django==3.1.14 3 | django-extensions==2.2.8 4 | djangorestframework==3.11.2 5 | pytz==2019.3 6 | six==1.14.0 7 | sqlparse==0.3.1 8 | --------------------------------------------------------------------------------