django休息框架查询器不排序 [英] django rest framework queryset doesn't order
本文介绍了django休息框架查询器不排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用元模型
order = ['-published_date']
现在看来:
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')
但是这个订单不行,它显示我订购ASC,而我需要DESC,它不影响订单
But this ordering doesn't work, it shows me ordering ASC, and i need DESC, it doesn't affect order at all.
我做错了什么?
推荐答案
解决方案是覆盖 filter_queryset
:
def filter_queryset(self, queryset):
queryset = super(InvoiceViewSet, self).filter_queryset(queryset)
return queryset.order_by('-published_date')
这篇关于django休息框架查询器不排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文