Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 847
gadss Avatar asked Mar 23 '12 04:03

gadss


People also ask

Can we order data in descending order in Django?

When we read the data from the model, Django returns queryset. Django has order_by method to sort the queryset in ascending and descending order. You can order the queryset on any field.

How do I sort by date in Django?

This answer Django Datetime Field Query - Order by Time/Hour suggests that I use '__day' with my date_modified as in: Article. objects. filter().

What is order_by in Django?

Django order by lets you specify how your query results should be ordered. Here's an example where we will order by the author's name: class Author(models.Model): name = models.CharField() Author.objects.order_by("name")

Does order of filter matter Django?

If you are just doing two simple filter operations, then you're correct that order doesn't matter, but be careful. There are examples of when the order of your queryset methods do matter: https://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-filter-clauses.


1 Answers

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

Notice the - before check_in.

Django Documentation

like image 91
Keith Avatar answered Dec 10 '22 22:12

Keith