Rebecca Martin
@laches1sm
PyConIE '18
# in settings.py
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.QueryParameterVersioning'
'DEFAULT_VERSION': 'v1' # Default is None
'ALLOWED_VERSIONS': ... # Default is None
'VERSION_PARAM': 'pyconie' # Default is 'version'
}
# in my_cool_function...
if req.version == 'v6':
return FancyNewSerializerV6
else if req.version == 'v2':
return OldSerializer
urlpatterns = [
url(
r'^(?P<version>(v1|v2))/bookings/$',
bookings_list,
name='bookings-list'
),
url(
r'^(?P<version>(v1|v2))/bookings/(?P<pk>[0-9]+)/$',
bookings_detail,
name='bookings-detail'
)
]
# bookings/urls.py
urlpatterns = [
url(r'^$', bookings_list, name='bookings-list'),
url(r'^(?P<pk>[0-9]+)/$', bookings_detail, name='bookings-detail')
]
# urls.py
urlpatterns = [
url(r'^v1/bookings/', include('bookings.urls', namespace='v1')),
url(r'^v2/bookings/', include('bookings.urls', namespace='v2'))
]
^([a-zA-Z0-9]+)\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$
"djangorestframework-version-transforms enables REST framework developers to remove version compatibility boilerplate from their endpoints. Developers need only write version compatibility once per version change, in the form of version transforms. Version transforms encapsulate the necessary changes to promote or demote a resource representation between versions. Endpoint logic can then only concern itself with the highest (or current) version of the resource, leading to great benefits in maintainability."
(from https://django-rest-framework-version-transforms.readthedocs.io/en/latest/)
As in the name...
Uses Transforms in order to convert between versions
class BaseTransform(object):
"""
All transforms should extend 'BaseTransform', overriding the two
methods '.forwards()' and '.backwards()' to provide forwards and backwards
conversions between representation versions.
"""
def forwards(self, data, request):
"""
Converts from this transform's base version to the next version of the representation.
:returns: Dictionary with the correct structure for the targeted version of the representation.
"""
raise NotImplementedError(".forwards() must be overridden.")
def backwards(self, data, request, instance):
"""
Converts from the next version back to this transform's base version of the representation.
:returns: Dictionary with the correct structure for the base version of the representation.
"""
raise NotImplementedError(".backwards() must be overridden.")