Search code examples
djangoapiresttastypie

How to get rid of the version information in API url in tastypie django?


I am creating REST based APIs for an app using Tastypie with Django. The problem is default API url in Tastypie contains version info in url patterns i.e.

http://lx:3001/api/v1/vservers/?username=someuser&api_key=someapikey

I want my url to be free from API version info like this:

http://lx:3001/api/vservers/?username=someuser&api_key=someapikey

urls.py

v1_api = Api()
v1_api.api_name = ''
v1_api.register(UserResource())
...
url(r'^api/', include(v1_api.urls)),

I am overwriting api_name with an empty string still

http://lx:3001/api/vservers/?username=someuser&api_key=someapikey does not work.

How can I get rid of the version info altogether?

Thanks..


Solution

  • Subclass Api and override urls to remove all the api_name-related bits:

    class MyApi(Api):
        @property
        def urls(self):
            """
            Provides URLconf details for the ``Api`` and all registered
            ``Resources`` beneath it.
            """
            pattern_list = [
                url(r"^%s$" % trailing_slash(), self.wrap_view('top_level'), name="api_top_level"),
            ]
    
            for name in sorted(self._registry.keys()):
                pattern_list.append((r"^/", include(self._registry[name].urls)))
    
            urlpatterns = self.override_urls() + patterns('',
                *pattern_list
            )
            return urlpatterns