django order_by query set, ascending and descending

PythonDjangoSorting

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Notice the - before check_in.

[Django Documentation][1]

[1]: https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by "Documentation"

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

You can also use the following instruction:

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

Solution 5 - Python

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]

Solution 6 - Python

It works removing .all():

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

Solution 7 - Python

  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.

Solution 8 - Python

67

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

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

Solution 9 - Python

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

Solution 10 - Python

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

Solution 11 - Python

This is working for me.

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

Solution 12 - Python

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.

Solution 13 - Python

You can try this

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

- (hyphen) is used to indicate descending orde.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongadssView Question on Stackoverflow
Solution 1 - PythonKeithView Answer on Stackoverflow
Solution 2 - PythonLeonardo.ZView Answer on Stackoverflow
Solution 3 - PythonThomas TurnerView Answer on Stackoverflow
Solution 4 - PythonPatrickView Answer on Stackoverflow
Solution 5 - Pythonanjaneyulubatta505View Answer on Stackoverflow
Solution 6 - PythonTony NguyenView Answer on Stackoverflow
Solution 7 - PythonVishvajit PathakView Answer on Stackoverflow
Solution 8 - Pythonuser13061886View Answer on Stackoverflow
Solution 9 - PythonAmin MirView Answer on Stackoverflow
Solution 10 - PythonOscar Garcia - OOSSView Answer on Stackoverflow
Solution 11 - Pythonbihari_gamerView Answer on Stackoverflow
Solution 12 - PythonMithun RanaView Answer on Stackoverflow
Solution 13 - PythonBercoveView Answer on Stackoverflow