Well i have the nex model
class DispatchListResource(ModelResource):
class Meta:
queryset = Dispatch.objects.all()
resource_name = 'dispatchlist'
allowed_methods = ['get']
authorization = Authorization()
authentication = AuthResourceAuthentication()
include_resource_uri = False
and i wanna get the last 10 rows order by 'creation_date' column DESC, so i do next
def get_object_list(self, request, *args, **kwargs):
list = Dispatch.objects.all().order_by('-creation_time')[:10]
return list
i get the next error
"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"
any ideas? what happens ?
Tastypie does the slicing and pagination for you and does some more filterting. You need to supply the basic query set. That why you should not do the slicing yourself. Leave the slicing for tastypie and control it using limit and offset when you do the query BTW. You can use ordering to set the default order, you don't need to insert this into the query set http://django-tastypie.readthedocs.org/en/latest/resources.html#ordering