Search code examples
javascriptdjangoangularjstastypierestangular

Using tastypie filters with angularjs and restangular


I am trying to use AngularJS + Restangular to interact with an API created in Django with Tastypie. I have successfully interacted with the API using example code found here as a starting point (shown below).

yourApp.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl("/api");
    RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
        var newResponse;
        if (operation === "getList") {
            newResponse = response.objects;
            newResponse.metadata = response.meta;
        } else {
            newResponse = response;
        }
        return newResponse;
    });
    RestangularProvider.setRequestSuffix('/?');
});

I would like to use Tastypie's filtering mechanism in my API calls, but these parameters are sent via the query string and not the URI. An example from the Tastypie docs: http://localhost:8000/api/v1/entry/?user__username=daniel

Apart from reconfiguring Restangular's setRequestSuffix option before each request, is there any clean way to apply Tastypie-style filters in the query string using Restangular?


Solution

  • From https://github.com/mgonto/restangular/issues/301#issuecomment-24273429

    // GET to /partners?where={agentID: 1}
    Restangular.all('partners').getList({where: '{agentID: 1}'});
    
    // GET to /partners/123?where={agentID: 1}
    Restangular.one('partners', 123).get({where: '{agentID: 1}'});
    

    Seems like the getList() does the trick here.