django order_by query set, ascending and descending

前端 未结 11 2028
攒了一身酷
攒了一身酷 2020-12-07 08:59

How can I order by descending my query set in django by date?

Reserved.objects.all().filter(client=client_id).order_by(\'check_in\')

I just

相关标签:
11条回答
  • 2020-12-07 09:22

    for ascending order:

    Reserved.objects.filter(client=client_id).order_by('check_in')
    

    for descending order:

    1.  Reserved.objects.filter(client=client_id).order_by('-check_in')
    

    or

    2.  Reserved.objects.filter(client=client_id).order_by('check_in')[::-1]
    
    0 讨论(0)
  • 2020-12-07 09:22
    Reserved.objects.filter(client=client_id).earliest('check_in')
    

    Or alternatively

    Reserved.objects.filter(client=client_id).latest('-check_in')
    

    Here is the documentations for earliest() and latest()

    0 讨论(0)
  • 2020-12-07 09:24

    This is working for me.

    latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]
    
    0 讨论(0)
  • 2020-12-07 09:25
    1. Ascending order

      Reserved.objects.all().filter(client=client_id).order_by('check_in')
      
    2. Descending order

      Reserved.objects.all().filter(client=client_id).order_by('-check_in')
      

    - (hyphen) is used to indicate descending order here.

    0 讨论(0)
  • 2020-12-07 09:25

    67

    Reserved.objects.filter(client=client_id).order_by('-check_in')

    '-' is indicates Descending order and for Ascending order just give class attribute

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