Restangular 查询 returns 所有项目
Restangular query returns all items
我的 api 是使用 Django Rest Framework 设置的,我觉得我正在与 Django 尾部斜线作斗争。似乎有一些我还没有弄清楚的组合。基本查询始终 returns api 端点处的所有对象。
代码如下:
// App config
var App = angular.module('App',['App.controllers','restangular','ngRoute','ngResource']);
App.config(function ($interpolateProvider, $httpProvider, $resourceProvider, RestangularProvider) {
RestangularProvider.setBaseUrl('/api/');
// Things I've tried
RestangularProvider.setBaseUrl('http://127.0.0.1:8000/api/');
RestangularProvider.setRequestSuffix('/');
// with suffix http://127.0.0.1:8000/api/tests/?value=404
// without suffix http://127.0.0.1:8000/api/tests?value=404
$resourceProvider.defaults.stripTrailingSlashes = false;
});
// In controller
// Items in database
// [{"itemID": 1,"value": 5,},{"itemID": 2,"value": 404,},{"itemID": 3,"value": 73,}]
var params = {'value': 404};
Restangular.all('tests').getList(params).then(function(items) {
var items = items
console.log(items)
});
// Even the standard $resource does the same thing.
var Foo = $resource('/api/tests', {value:'@somevalue'});
$scope.allItems = {};
Foo.query({value:404}, function(items) {
$scope.allItems = items;
});
我可以看到它试图转到 /tests?params 但它出错了 tests/?params
"GET /api/tests?value=404 HTTP/1.1" 301 0
"GET /api/tests/?value=404 HTTP/1.1" 200 361
也许我的查询结构有误?有没有办法通过实际访问地址来测试查询?技术上不应该导航到 http://127.0.0.1:8000/api/tests?value=404 bring up in DRF only the list of objects with a value of 404? DRF puts in the slash at the end of the url before the parameters (http://127.0.0.1:8000/api/tests/?value=404).
有没有人有使用 Django Rest Framework 的可靠方法?
这是我的发现。查询需要 Django Rest Framework filtering。安装 django 过滤器后,将 filter_backends 和 filter_fields 添加到视图集中。很高兴。
$ pip install django-filter
// In your serializers.py
// Create your serializer
from rest_framework import serializers
class FoobarSerializer(serializers.ModelSerializer):
class Meta:
model = Foobar
fields = ('foo','bar')
// -----
// In your main app urls.py (or wherever you put your viewset, I have them in urls so most api stuff is collected there)
from rest_framework import routers
from rest_framework import viewsets
from rest_framework import filters
from mainapp.serializers import FoobarSerializer
from app.models import Foobar
// Put your filter fields in your viewset
class FoobarViewSet(viewsets.ModelViewSet):
queryset = Foobar.objects.all()
serializer_class = FoobarSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('foo', 'bar')
// Register the viewset
router = routers.DefaultRouter()
router.register(r'foobars', FoobarViewSet)
urlpatterns = [
...
url(r'^api/', include(router.urls)),
]
// -----
// In your angular controller.js
// Restangular
var params = {'foo': 404};
Restangular.all('foobars').getList(params).then(function(items) {
$scope.items = items
});
// $resource
var Foo = $resource('/api/foobars', {value:'@somevalue'});
Foo.query({value:404}, function(items) {
$scope.items = items;
});
// $http
$http({method:'GET',url:'/api/foobars',params:{value:404}}).then(function(response) {
$scope.items = response.data;
});
我的 api 是使用 Django Rest Framework 设置的,我觉得我正在与 Django 尾部斜线作斗争。似乎有一些我还没有弄清楚的组合。基本查询始终 returns api 端点处的所有对象。
代码如下:
// App config
var App = angular.module('App',['App.controllers','restangular','ngRoute','ngResource']);
App.config(function ($interpolateProvider, $httpProvider, $resourceProvider, RestangularProvider) {
RestangularProvider.setBaseUrl('/api/');
// Things I've tried
RestangularProvider.setBaseUrl('http://127.0.0.1:8000/api/');
RestangularProvider.setRequestSuffix('/');
// with suffix http://127.0.0.1:8000/api/tests/?value=404
// without suffix http://127.0.0.1:8000/api/tests?value=404
$resourceProvider.defaults.stripTrailingSlashes = false;
});
// In controller
// Items in database
// [{"itemID": 1,"value": 5,},{"itemID": 2,"value": 404,},{"itemID": 3,"value": 73,}]
var params = {'value': 404};
Restangular.all('tests').getList(params).then(function(items) {
var items = items
console.log(items)
});
// Even the standard $resource does the same thing.
var Foo = $resource('/api/tests', {value:'@somevalue'});
$scope.allItems = {};
Foo.query({value:404}, function(items) {
$scope.allItems = items;
});
我可以看到它试图转到 /tests?params 但它出错了 tests/?params
"GET /api/tests?value=404 HTTP/1.1" 301 0
"GET /api/tests/?value=404 HTTP/1.1" 200 361
也许我的查询结构有误?有没有办法通过实际访问地址来测试查询?技术上不应该导航到 http://127.0.0.1:8000/api/tests?value=404 bring up in DRF only the list of objects with a value of 404? DRF puts in the slash at the end of the url before the parameters (http://127.0.0.1:8000/api/tests/?value=404).
有没有人有使用 Django Rest Framework 的可靠方法?
这是我的发现。查询需要 Django Rest Framework filtering。安装 django 过滤器后,将 filter_backends 和 filter_fields 添加到视图集中。很高兴。
$ pip install django-filter
// In your serializers.py
// Create your serializer
from rest_framework import serializers
class FoobarSerializer(serializers.ModelSerializer):
class Meta:
model = Foobar
fields = ('foo','bar')
// -----
// In your main app urls.py (or wherever you put your viewset, I have them in urls so most api stuff is collected there)
from rest_framework import routers
from rest_framework import viewsets
from rest_framework import filters
from mainapp.serializers import FoobarSerializer
from app.models import Foobar
// Put your filter fields in your viewset
class FoobarViewSet(viewsets.ModelViewSet):
queryset = Foobar.objects.all()
serializer_class = FoobarSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('foo', 'bar')
// Register the viewset
router = routers.DefaultRouter()
router.register(r'foobars', FoobarViewSet)
urlpatterns = [
...
url(r'^api/', include(router.urls)),
]
// -----
// In your angular controller.js
// Restangular
var params = {'foo': 404};
Restangular.all('foobars').getList(params).then(function(items) {
$scope.items = items
});
// $resource
var Foo = $resource('/api/foobars', {value:'@somevalue'});
Foo.query({value:404}, function(items) {
$scope.items = items;
});
// $http
$http({method:'GET',url:'/api/foobars',params:{value:404}}).then(function(response) {
$scope.items = response.data;
});