You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django中按date_created对排除指定状态的QuerySet排序?

Fixing the QuerySet Sorting for Your Django Order Model

No problem at all—let's get that sorting working correctly. The issue here is your original query references a non-existent field order_date1, while your Order model has the valid date_created field you intended to use for sorting.

Corrected Query

Here's the updated code that filters and sorts your orders properly:

all_order = Order.objects.exclude(status='new order').order_by('date_created')

Bonus: Reverse (Descending) Order

If you want to sort from the most recently created order to the oldest (descending order), just prepend a - to the field name:

all_order = Order.objects.exclude(status='new order').order_by('-date_created')

A quick note: since your date_created field uses auto_now_add=True, it’s automatically set when an Order instance is created—so this sorting will reliably reflect the actual creation time of each order.

内容的提问来源于stack exchange,提问作者Red Person

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:17:43