[英]Using tastypie filters with angularjs and restangular
I am trying to use AngularJS + Restangular to interact with an API created in Django with Tastypie. 我正在尝试使用AngularJS + Restangular与使用Deliciouspie在Django中创建的API进行交互。 I have successfully interacted with the API using example code found here as a starting point (shown below). 我已使用此处找到的示例代码作为起点成功与API进行了交互(如下所示)。
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. 我想在我的API调用中使用Tastypie的过滤机制,但是这些参数是通过查询字符串而不是URI发送的。 An example from the Tastypie docs : http://localhost:8000/api/v1/entry/?user__username=daniel 来自Tastypie文档的示例: 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? 除了在每个请求之前重新配置Restangular的setRequestSuffix选项外,是否有任何干净的方法可以使用Restangular在查询字符串中应用Tastypie样式的过滤器?
From https://github.com/mgonto/restangular/issues/301#issuecomment-24273429 来自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. 好像getList()在这里起到了作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.