2014-03-04 39 views
0

嗯,我的NEX機型Django的tastypie限制ñ

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

Tastypie爲你做了切片和分頁,並做了一些更過濾。您需要提供基本的查詢集。 那你爲什麼不應該自己去切片。當你做查詢 順便說一句,離開切片tastypie和控制它使用限制和偏移量。您可以使用訂單來設置默認訂單,您不需要將其插入查詢集http://django-tastypie.readthedocs.org/en/latest/resources.html#ordering