Skip to content
Advertisement

How can I sort my Django Rest Framework results?

I have tried doing this with dynamic-rest and couldn’t get it to work, now I am trying with the built in ordering method in django rest-framework, still can’t get it to work.

I have a feeling it is the method I am using get get these results from the database, but I do not know of any other way, so if anyone knows how to get this working, in any way, please let me know, thank you!

models.py

JavaScript

views.py

JavaScript

I have tried setting ordering/ordering_fields to notifications_set.pk, this does not work either.

serializers.py

JavaScript

This does not work with a default ordering specified (with ordering flag in view), or with ?ordering=field in the GET request

Advertisement

Answer

you are trying to order by this field notifications, this field not exists in your model fields, you should use one already exist

and you can order also using ORM like this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement