我在Django遇到了一些问题。不幸的是,我不能使用内置在分页中的Django REST框架,因为它添加了诸如“next”、“previous”和“count”之类的未经验证的JSON字段,所以我想限制在queryset上使用[:10]选项。在
在我下面的尝试中,我得到了一个错误:
Queryset Cannot reorder a query once a slice has been taken.class LocationsViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = serializers.LocationSerializer
ordering = ('location_name',)
def get_queryset(self):
queryset = models.GeonamesLocation.objects.all()
name_prefix = self.request.QUERY_PARAMS.get('name_prefix', None)
if name_prefix is not None:
if len(name_prefix) < 3:
raise exceptions.ParseError("'name_prefix' must consist of at least 2 characters")
queryset = queryset.filter(location_name__icontains = name_prefix).order_by('location_name', 'geonames_country', 'geonames_region')[:10]
return queryset
我理解为什么我在使用order-by之前应用了[:10],那么为什么会出现这个错误,但是既然我是在之后执行的,它为什么会给我这个错误呢?在
谢谢,
马克