877๐
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".
89๐
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
- [Django]-Substring in a django template?
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-Django โ Annotate multiple fields from a Subquery
45๐
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',)
- [Django]-Pagination in Django-Rest-Framework using API-View
- [Django]-Add rich text format functionality to django TextField
- [Django]-How to query as GROUP BY in Django?
31๐
You can also use the following instruction:
Reserved.objects.filter(client=client_id).order_by('check_in').reverse()
- [Django]-Django south migration โ Adding FULLTEXT indexes
- [Django]-Django Sitemaps and "normal" views
- [Django]-How to implement followers/following in Django
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]
- [Django]-How to monkey patch Django?
- [Django]-Django admin default filter
- [Django]-VueJS + Django Channels
9๐
-
Ascending order
Reserved.objects.all().filter(client=client_id).order_by('check_in')
-
Descending order
Reserved.objects.all().filter(client=client_id).order_by('-check_in')
-
(hyphen) is used to indicate descending order here.
- [Django]-__init__() got an unexpected keyword argument 'mimetype'
- [Django]-Celery. Decrease number of processes
- [Django]-What is a "slug" in Django?
8๐
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
- [Django]-What is more efficient .objects.filter().exists() or get() wrapped on a try
- [Django]-Django QuerySet order
- [Django]-What is the difference render() and redirect() in Django?
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.
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-'pip' is not recognized as an internal or external command
- [Django]-Import data from excel spreadsheet to django model
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()
- [Django]-Django: remove a filter condition from a queryset
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-How to change empty_label for modelForm choice field?
2๐
This is working for me.
latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]
- [Django]-Django template can't see CSS files
- [Django]-How can I activate the unaccent extension on an already existing model
- [Django]-Combining Django F, Value and a dict to annotate a queryset
2๐
You can try this
Staffs.objects.filter(active=1).order_by('rank')
โ (hyphen) is used to indicate descending orde.
- [Django]-Error when using django.template
- [Django]-Disable session creation in Django
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
1๐
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')
- [Django]-Create custom buttons in admin change_form in Django
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-Get count of related model efficiently in Django