Skip to content

Instantly share code, notes, and snippets.

View settings_rest_framework.py
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning'
}
View django_blog_api_urls.py
urlpatterns = [
path("admin/", admin.site.urls),
re_path(r'api/(?P<version>[v1|v2]+)/', include('django_blog.apps.blog.rest_api.urls')),
]
View settings_installed_apps.py
INSTALLED_APPS = [
....
# our apps
"django_blog.apps.common.apps.CommonConfig",
"django_blog.apps.account.apps.AccountConfig",
"django_blog.apps.blog.apps.BlogConfig",
]
View django_blog_db_url.env
DJANGO_BLOG_DATABASE_URL=psql://django_blog@127.0.0.1:5432/django_blog
View gist:cf0b48e9aa51c8a2dab9587a4b30f369
django_blog
├── api
│   ├── django_blog
│   │   ├── apps
│   │   │   ├── account
│   │   │   │   ├── admin.py
│   │   │   │   ├── apps.py
│   │   │   │   ├── forms.py
│   │   │   │   ├── __init__.py
│   │   │   │   ├── managers.py
View urls.py
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions
schema_view = get_schema_view(
openapi.Info(
title="Blog API",
default_version='v1',
View versioning_example.py
def get_serializer_class(self):
if self.request.version == 'v2':
return PostSerializerV2
else:
return super().get_serializer_class()
View shell
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.......
----------------------------------------------------------------------
Ran 7 tests in 0.036s
OK
Destroying test database for alias 'default'...