Django date query from newest to oldest

后端 未结 2 1617
北恋
北恋 2021-02-19 09:27

I am building my first Django program from scratch and am running into troubles trying to print out items to the screen from newest to oldest. My model has an auto date time fie

相关标签:
2条回答
  • 2021-02-19 09:48

    By the way you also have Django's created_at field at your disposal:

    ordered_tasks = TaskItem.objects.order_by('-created_at')
    
    0 讨论(0)
  • 2021-02-19 09:52
    ordered_tasks = TaskItem.objects.order_by('-created_date')
    

    The order_by() method is used to order a queryset. It takes one argument, the attribute by which the queryset will be ordered. Prefixing this key with a - sorts in reverse order.

    0 讨论(0)
提交回复
热议问题