I am porting my API from Piston to TastyPie. We have existing clients, so we want to keep the url structure of the API the same.

My site's top level urls.py conf looks like this:

    urlpatterns = patterns('',
          ........LOTS OF STUFF

          (r'^v1/', include('api.urls')),

Using Piston, every individual resource is then registered independently in api.urls and added to the urlpatterns, so you might see /v1/deals/ for example.

Now, I'm having some issues recreating this structure with TastyPie. At its top level, the Tastypie Api() object's urlpatterns expects a capturing group called "api_name". By default, v1 is hardcoded as that top level pattern, though you can override it by passing "api_name" as a keyword argument when instantiating Api().

My current api.urls.py looks like this (with tastypie):

   v1_api = Api()
   v1_api.register(DealResource())

   urlpatterns = patterns('',
       (r'^', include(v1_api.urls)),

)

The result is that the pattern to be matched for the API is now '/v1/v1/[resource_name]/'.

One issue is that I don't see how I can remove ^v1/' from the top level url conf. Our index page matches '^$', so I can't just go that route.

I suppose I could just register individual modelresources, rather than the api object. That seems suboptimal though. Am I wrong?

I've also considered subclassing the Tastypie Api object and removing the "api_name" capturing group.

Any thoughts?

有帮助吗?

解决方案

I believe the best approach is still to remove the 'v1' prefix from the top-level urlconf. Something along the lines of:

urlpatterns = patterns('',
    ........LOTS OF STUFF
    (r'^$', 'app.views.home_page'),
    (r'', include('api.urls')),
)

This way, empty requests will map to your homepage while API requests will go on to your api.urls config.

Another way to go about this is to simply add the API urls to the urlpatterns instance directly:

urlpatterns = patterns('',
    ........LOTS OF STUFF
)

urlpatterns += api.urls.ulrpatterns
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top