ChatGPT解决这个技术问题 Extra ChatGPT

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.


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

Notice the - before check_in.

Django Documentation


models.somModalName.all().order_b('-date/time')
- before column name mean descending order without - mean ascending.
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?
n
nandhp
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


S
Samsul Islam

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

C
Community

You can also use the following instruction:

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

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.
a
anjaneyulubatta505

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]

Why would anyone ever want to use the 2nd method?
That's one of the available choices. But, better approach is to use 1st method.
The 2nd one will return a list, not a queryset.
@ruohola yes, it will return list
A
Andrea

It works removing .all():

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

This is essentially identical to @leonardo-z's answer, is it not?
d
double-beep

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.


You could save some queries by just calling filter
S
Samsul Islam

67

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

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


Please give more context for your questions
Hi, welcome on StackOverflow, please review your answer, format it correctly, explain it (what is this "67" on top ???) ; you may refer to stackoverflow.com/help/how-to-answer to learn how to write a "good" answer
A
Amin Mir
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()


O
Oscar Garcia - OOSS

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


M
Mithun Rana

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.


b
bihari_gamer

This is working for me.

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

B
Bercove

You can try this

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

- (hyphen) is used to indicate descending orde.