Python django rest 框架查询集不排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24987446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django rest framework queryset doesn't order
提问by Mirza Delic
i use model with Meta
ordering = ['-published_date']
我使用模型 Meta
ordering = ['-published_date']
Now in view:
现在看来:
class InvoiceViewSet(viewsets.ModelViewSet):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
filter_fields = ('table',)
And serializer:
和序列化器:
class InvoiceSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True, allow_add_remove=True)
class Meta:
model = Invoice
fields = ('id', 'items', 'table', 'published_date')
But this ordering doesn't work, it shows me ordering ASC, and i need DESC, it doesn't affect order at all.
但是这个排序不起作用,它显示我在订购 ASC,而我需要 DESC,它根本不影响订单。
What am i doing wrong?
我究竟做错了什么?
采纳答案by Mirza Delic
Solution is to override filter_queryset
:
解决方案是覆盖filter_queryset
:
def filter_queryset(self, queryset):
queryset = super(InvoiceViewSet, self).filter_queryset(queryset)
return queryset.order_by('-published_date')
回答by Tom Christie
If your model does have an ordering it reallywill be reflected in the list view by default. I'd suggest overriding get_queryset()
and debugging the return result there, or else explicitly adding the ordering to the queryset.
如果您的模型确实有排序,那么默认情况下它确实会反映在列表视图中。我建议get_queryset()
在那里覆盖和调试返回结果,或者明确地将排序添加到查询集中。
For example:
例如:
queryset = Invoice.objects.all().order_by('-published_date')
Wondering if it's possible you've configured a filter that's overriding the ordering. Worth testing what happens if you turn all filters off. I see you have the filter_fields
attribute set, so assuming you've got something like this in your settings...
想知道您是否可能配置了一个覆盖排序的过滤器。值得测试一下,如果关闭所有过滤器会发生什么。我看到你filter_fields
设置了属性,所以假设你的设置中有这样的东西......
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
}
If you comment that out does that fix things?
如果您对此发表评论,是否可以解决问题?
回答by EdgarT
@Mirza Delic answer works but does not keep the ordering comming from request.QUERY_PARAMS.
@Mirza Delic 答案有效,但不会保留来自 request.QUERY_PARAMS 的排序。
class YOUR_VIEW_SET(viewsets.ModelViewSet):
#your code here
ordering_filter = OrderingFilter()
def filter_queryset(self, queryset):
queryset = super(YOUR_VIEW_SET, self).filter_queryset(queryset)
return self.ordering_filter.filter_queryset(self.request, queryset, self)
This works for me and for other people I hope.
这对我和我希望的其他人都有效。
回答by shiva reddy
For Django REST Framework you can use OrderingFilter.
对于 Django REST 框架,您可以使用OrderingFilter。
from django_filters import DjangoFilterBackend
from rest_framework import viewsets, filters
class InvoiceViewSet(viewsets.ModelViewSet):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
# Explicitly specify which fields the API may be ordered against
ordering_fields = ('items', 'table', 'published_date')
# This will be used as the default ordering
ordering = ('-published_date')