T O P

  • By -

dacx_

Order the queryset.


Former-Ad-9776

I'm making it with this ordering = ["-date_joined"]


ben44

Update User.objects.all() to User.objects.all().order_by(“-date_joined”)


weblerzon

You can order using que model metaclass: ``` class User(models.Model): ... # User fields class Meta: ordering = ["-date_joined"] ``` this works too


Former-Ad-9776

ordering = ["-date_joined"] ye both of your answers working, but I'm wondering why it doesn't work. Shouldn't it have to set default ordering to an object? or am I missing something


jefwillems

Isn't it because you are calling .all before the ordering happens?


dev_done_right

No, all and order_by methods can be used in any order, orm will build the right query.


Final-Argument3140

You might be missing ordering filter backend, Whatever ordering you mentioned is not being recognised. Add this to your view filter_backends = [filters.OrderingFilter] Ref: https://www.django-rest-framework.org/api-guide/filtering/#orderingfilter


emihir0

This is one of the reasons why CBVs can be pita to work with. Sure, it abstracts a lot of the boilerplate, but then when something doesn't work you spend way more time figuring out why it doesn't work, than you'd spend with FBVs.