3

i have historic data that i want to publish via webservice by versions. To do that i'm using Django-Rest-Framework. I'm already using this framework to provide other services but now it seems to be a little bit harder to accomplished this goal.

The main goal is to provide a url:

http://127.0.0.1:8000/service/vocab (realtime) - done
http://127.0.0.1:8000/service/v1/vocab (version 1)
http://127.0.0.1:8000/service/v2/vocab (version 2)
http://127.0.0.1:8000/service/vn/vocab (version n)

for this i'm trying to config the DRF router to make this possible.

So the idea is something like this:

urls.py

router = routers.DefaultRouter()
router.register(r'vocab', views.VocabViewSet, 'vocabs')
router.register(r'{version}/vocab', views.VersionViewSet, 'vocab')

    urlpatterns = patterns('',
    ...
        url(r'^service/', include(router.urls))


)

views.py

class VersionViewSet(viewsets.ModelViewSet):
        queryset = Version.objects.all()
        serializer_class = VersionSerializer

        @detail_route(methods=['post'], url_path='vocab')
            def get_vocabs(self, request, version='v1'):
            queryset = Version.objects.filter(version=version)

In this case it occurs :

invalid literal for int() with base 10: 'version'

That's because that DRF expects an int after service/.

I'm trying to find a solution to this case. Can you provide any hint how can i make this ?

Maybe Customize dynamic routes is the a good approach, what do you think ? If so, can you provide an example how to apply it in this case or similar?

Thanks in advance.

2 Answers 2

1

The correct answer to this is URL versioning, which is automatically supported by Django Rest Framework. You can find all the details you need here:

http://www.django-rest-framework.org/api-guide/versioning/

In your particular case, you'd want to use the URLPathVersioning. You can start by adding this key-value pair to your REST_FRAMEWORK settings:

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning'
}

then, configure your urls.py similar to this:

http://www.django-rest-framework.org/api-guide/versioning/#urlpathversioning

Sign up to request clarification or add additional context in comments.

Comments

0

You can have a url folder with multiple files in it (__init__.py, urls_api_1_0.py and urls_api_1_0.py). Basically one file for every API version and your __init__.py file. Which routes your different API versions. See code below

import urls_api_1_0
import urls_api_2_0

urlpatterns = patterns('',
    url(r'^api/v1/', include(urls_api_1_0)),
    url(r'^api/v2/', include(urls_api_2_0)),
)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += format_suffix_patterns(urlpatterns, allowed=['json', 'html'])

Your urls_api_1_0.py could look something like this

from django.conf.urls import patterns, url
import package.views.api_2_0 as views

urlpatterns = \
patterns('',
         url(r'^ vocab/$', views.VocabListView.as_view(),
             name='all-vocab-v2'),
         )

1 Comment

This is not dynamic. If you in backoffice define a third version? How you will define? you will have to go to urls.py and define a new url pattern?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.