我有nex模型
class DispatchListResource(ModelResource):
class Meta:
queryset = Dispatch.objects.all()
resource_name = 'dispatchlist'
allowed_methods = ['get']
authorization = Authorization()
authentication = AuthResourceAuthentication()
include_resource_uri = False
我希望通过'creation_date'列DESC获得最后10行的顺序,所以我接下来
def get_object_list(self, request, *args, **kwargs):
list = Dispatch.objects.all().order_by('-creation_time')[:10]
return list
我收到下一个错误
"error_message": "Cannot filter a query once a slice has been taken.",
"traceback": "Traceback (most recent call last):\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 195, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 426, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 458, in dispatch\n response = method(request, **kwargs)\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 1277, in get_list\n bundles.append(self.full_dehydrate(bundle, for_list=True))\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 840, in full_dehydrate\n bundle = self.dehydrate(bundle)\n\n File \"C:\\Users\\dabanto\\proyectos\\taxitrack\\api\\interface.py\", line 105, in dehydrate\n bundle, dispatch_id=bundle.data['dispatch_id']\n\n File \"C:\\Python26\\lib\\site-packages\\django_tastypie-0.11.0-py2.6.egg\\tastypie\\resources.py\", line 2060, in obj_get\n object_list = self.get_object_list(bundle.request).filter(**kwargs)\n\n File \"C:\\Python26\\lib\\site-packages\\django\\db\\models\\query.py\", line 669, in filter\n return self._filter_or_exclude(False, *args, **kwargs)\n\n File \"C:\\Python26\\lib\\site-packages\\django\\db\\models\\query.py\", line 681, in _filter_or_exclude\n \"Cannot filter a query once a slice has been taken.\"\n\nAssertionError: Cannot filter a query once a slice has been taken.\n"
任何想法?会发生什么?
答案 0 :(得分:0)
Tastypie为你做切片和分页,并做一些更多的过滤。您需要提供基本查询集。 这就是为什么你不应该自己做切片。保留切片以进行tastypie并在执行查询时使用limit和offset来控制它 BTW。您可以使用排序来设置默认顺序,您无需将其插入查询集http://django-tastypie.readthedocs.org/en/latest/resources.html#ordering