regex - Django-Rest-Framework dynamic url (dynamic prefix) -


i have historic data want publish via webservice versions. i'm using django-rest-framework. i'm using framework provide other services seems little bit harder accomplished goal.

the main goal provide 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 i'm trying config drf router make possible.

so idea 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 case occurs :

invalid literal int() base 10: 'version' 

that's because drf expects int after service/.

i'm trying find solution case. can provide hint how can make ?

maybe customize dynamic routes approach, think ? if so, can provide example how apply in case or similar?

thanks in advance.

the correct answer url versioning, automatically supported django rest framework. can find details need here:

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

in particular case, you'd want use urlpathversioning. can start adding key-value pair rest_framework settings:

rest_framework = {     'default_versioning_class': 'rest_framework.versioning.urlpathversioning' } 

then, configure urls.py similar this:

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


Comments