476

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 want to filter from descending all the Reserved by check_in date.

iTayb
  • 12,373
  • 24
  • 81
  • 135
gadss
  • 21,687
  • 41
  • 104
  • 154

12 Answers12

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

Notice the - before check_in.

Django Documentation

Keith
  • 9,278
  • 1
  • 15
  • 7
  • 1
    models.somModalName.all().order_b('-date/time') – Shedrack Apr 29 '20 at 10:32
  • 17
    `-` before column name mean descending order without `-` mean ascending. – CallMarl Jun 03 '20 at 08:52
  • 1
    what if i want to order by query param? I mean if "order=asc" then i want to order by ascending and if "order=desc" then order by descending! Instead of using "if/else" is there any way to pass **asc** or **desc** to any parameter and then order according to that? – Shahriar Rahman Zahin Dec 29 '20 at 07:56
88
Reserved.objects.filter(client=client_id).order_by('-check_in')

A hyphen "-" in front of "check_in" indicates descending order. Ascending order is implied.

We don't have to add an all() before filter(). That would still work, but you only need to add all() when you want all objects from the root QuerySet.

More on this here: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters

nandhp
  • 4,680
  • 2
  • 30
  • 42
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
44

Adding the - will order it in descending order. You can also set this by adding a default ordering to the meta of your model. This will mean that when you do a query you just do MyModel.objects.all() and it will come out in the correct order.

class MyModel(models.Model):

    check_in = models.DateField()

    class Meta:
        ordering = ('-check_in',)
Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
Thomas Turner
  • 2,722
  • 1
  • 27
  • 23
31

You can also use the following instruction:

Reserved.objects.filter(client=client_id).order_by('check_in').reverse()
Community
  • 1
  • 1
Patrick
  • 1,091
  • 1
  • 14
  • 14
  • 9
    You can, but I strongly suspect that it be more efficient to let the SQL server handle the order, at least in theory. It's nice and clear, though. – Michael Scheper Sep 26 '16 at 16:51
20

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]
anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
9
  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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Vishvajit Pathak
  • 3,351
  • 1
  • 21
  • 16
7

This is very easy and simple just follow the below instruction.

----- This for Descending

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

------This for Ascending

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

if you want to select by Descending just add minus operator before the attribute field or if you want to select by Ascending no need minus operator.

Mithun Rana
  • 1,334
  • 1
  • 9
  • 10
6

If for some reason you have null values you can use the F function like this:

from django.db.models import F

Reserved.objects.all().filter(client=client_id).order_by(F('check_in').desc(nulls_last=True))

So it will put last the null values. Documentation by Django: https://docs.djangoproject.com/en/stable/ref/models/expressions/#using-f-to-sort-null-values

6
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()

Amin Mir
  • 640
  • 8
  • 15
2

This is working for me.

latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]
bihari_gamer
  • 422
  • 5
  • 15
2

You can try this

Staffs.objects.filter(active=1).order_by('rank')

- (hyphen) is used to indicate descending orde.

Bercove
  • 987
  • 10
  • 18
0

Order By Ascending:

Structure:

Model.objects.filter(model_column_name=model_column_value).order_by(expected_column_name as string)

Example:

Employee.objects.filter(department=department_id).order_by('salary')

Order By Descending:

Structure:

Model.objects.filter(model_column_name=model_column_value).order_by(-expected_column_name as string)

Example:

Employee.objects.filter(department=department_id).order_by('-salary')
MD. SHIFULLAH
  • 913
  • 10
  • 16