【发布时间】:2014-09-19 04:19:42
【问题描述】:
我用Metaordering = ['-published_date']的模型
现在可见:
class InvoiceViewSet(viewsets.ModelViewSet):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
filter_fields = ('table',)
和序列化器:
class InvoiceSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True, allow_add_remove=True)
class Meta:
model = Invoice
fields = ('id', 'items', 'table', 'published_date')
但是这个排序不起作用,它显示我在排序 ASC,我需要 DESC,它根本不影响排序。
我做错了什么?
【问题讨论】:
-
如果您从 shell 调用
Invoice.objects.all()是否显示 DESC 排序? -
是的。这很奇怪。 ://
-
它必须与 DRF 有关。你的输出是什么样的?
-
我用rest客户端来查看输出,它只是按ASC排序。视图或序列化程序可能有问题吗?或者有没有其他方法可以做到这一点?
-
您可以覆盖
get_queryset()方法,但是,我之前已经这样做过,就像您一样,它对我有用。其他的一定是错的。
标签: python django django-rest-framework