如何在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
相关产品推荐
相关产品推荐

