gadss
gadss

Reputation: 22489

django order_by query set, ascending and descending

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.

Upvotes: 499

Views: 616572

Answers (13)

dev_light
dev_light

Reputation: 3879

You can use the ordering option in the model’s Meta to order your query set. To order in descending order, use a leading "-":

class ModelName(models.Model):
    check_in = models.DateTimeField(auto_now_add = True)

    class Meta:
        ordering = ["-check_in"]

Fields without a leading “-” will be ordered ascending. Use the string “?” to order randomly.

For example, to order by check_in field ascending, use this:

ordering = ["check_in"]

To order by check_in descending, use this:

ordering = ["-check_in"]

See the docs.

Upvotes: 0

Oscar Garcia - OOSS
Oscar Garcia - OOSS

Reputation: 351

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

Upvotes: 7

Keith
Keith

Reputation: 9708

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

Notice the - before check_in.

- before column name mean "descending order", while without - mean "ascending".

Django Documentation

Upvotes: 906

MD. SHIFULLAH
MD. SHIFULLAH

Reputation: 1721

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')

Upvotes: 3

Bercove
Bercove

Reputation: 1175

You can try this

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

- (hyphen) is used to indicate descending orde.

Upvotes: 3

Mithun Rana
Mithun Rana

Reputation: 1382

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.

Upvotes: 7

Thomas Turner
Thomas Turner

Reputation: 3042

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',)

Upvotes: 49

Amin Mir
Amin Mir

Reputation: 642

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

Upvotes: 6

Patrick
Patrick

Reputation: 1111

You can also use the following instruction:

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

Upvotes: 31

Vishvajit Pathak
Vishvajit Pathak

Reputation: 3711

  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.

Upvotes: 9

manny8086
manny8086

Reputation: 509

This is working for me.

latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]

Upvotes: 2

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11665

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]

Upvotes: 20

Leonardo.Z
Leonardo.Z

Reputation: 9781

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

Upvotes: 90

Related Questions