如何使用 django-rest-framework 和 ng-admin 进行分页
How to paginate with django-rest-framework and ng-admin
抱歉,如果这是一个愚蠢的问题,因为它包含在文档中,但我无法让它工作(我是 AngularJS 的新手)。
我正在尝试在 django-rest-framework 之上使用 ng-admin 为我的数据库编写一个接口。问题是 ng-admin 中的默认分页类似于 _page=1&_perPage=30
,而在 django-rest-framework 中它是 ?limit=30&offset=1
。因此,当 ng-admin 尝试获取它时,它会收到 301
响应。在文档中它说:
For instance, to use offset and limit instead of _page and _perPage across the entire application, use the following code:
myApp.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
if (operation == 'getList' && what == 'entityName') {
params.offset = (params._page - 1) * params._perPage;
params.limit = params._perPage;
delete params._page;
delete params._perPage;
}
return { params: params };
});
}]);
问题是我不知道如何将其合并到我的代码中,即:
var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', function (nga) {
var admin = nga.application('My First Admin')
.baseApiUrl('http://localhost:8000/CSP_site/');
var algae = nga.entity('algae');
algae.listView().fields([
nga.field('name'),
nga.field('id'),
]);
admin.addEntity(algae);
nga.configure(admin);
}]);
任何提示将不胜感激。
谢谢!
这应该为您完成:
var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', 'RestangularProvider', function (nga, RestangularProvider) {
var admin = nga.application('My First Admin')
.baseApiUrl('http://localhost:8000/CSP_site/');
var algae = nga.entity('algae');
algae.listView().fields([
nga.field('name'),
nga.field('id'),
]);
admin.addEntity(algae);
nga.configure(admin);
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
if (operation == 'getList' && what == 'algae') {
params.offset = (params._page - 1) * params._perPage;
params.limit = params._perPage;
delete params._page;
delete params._perPage;
}
return { params: params };
});
}]);
在数组的第二个索引中添加了 RestangularProvider
,它将其作为第二个参数注入到 .config
函数中。
除了what == 'entityName'
,其余都是复制粘贴,变成了what == 'algae'
,因为algae
是你上面添加的实体。
希望对您有所帮助。
抱歉,如果这是一个愚蠢的问题,因为它包含在文档中,但我无法让它工作(我是 AngularJS 的新手)。
我正在尝试在 django-rest-framework 之上使用 ng-admin 为我的数据库编写一个接口。问题是 ng-admin 中的默认分页类似于 _page=1&_perPage=30
,而在 django-rest-framework 中它是 ?limit=30&offset=1
。因此,当 ng-admin 尝试获取它时,它会收到 301
响应。在文档中它说:
For instance, to use offset and limit instead of _page and _perPage across the entire application, use the following code:
myApp.config(['RestangularProvider', function(RestangularProvider) { RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) { if (operation == 'getList' && what == 'entityName') { params.offset = (params._page - 1) * params._perPage; params.limit = params._perPage; delete params._page; delete params._perPage; } return { params: params }; }); }]);
问题是我不知道如何将其合并到我的代码中,即:
var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', function (nga) {
var admin = nga.application('My First Admin')
.baseApiUrl('http://localhost:8000/CSP_site/');
var algae = nga.entity('algae');
algae.listView().fields([
nga.field('name'),
nga.field('id'),
]);
admin.addEntity(algae);
nga.configure(admin);
}]);
任何提示将不胜感激。
谢谢!
这应该为您完成:
var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', 'RestangularProvider', function (nga, RestangularProvider) {
var admin = nga.application('My First Admin')
.baseApiUrl('http://localhost:8000/CSP_site/');
var algae = nga.entity('algae');
algae.listView().fields([
nga.field('name'),
nga.field('id'),
]);
admin.addEntity(algae);
nga.configure(admin);
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
if (operation == 'getList' && what == 'algae') {
params.offset = (params._page - 1) * params._perPage;
params.limit = params._perPage;
delete params._page;
delete params._perPage;
}
return { params: params };
});
}]);
在数组的第二个索引中添加了 RestangularProvider
,它将其作为第二个参数注入到 .config
函数中。
除了what == 'entityName'
,其余都是复制粘贴,变成了what == 'algae'
,因为algae
是你上面添加的实体。
希望对您有所帮助。