Skip to content
Advertisement

How to return a string expression for order_by args in django queryset?

Let’s say I want to do this in the views of a Django project:

JavaScript

I expect the order_by_query to be a string like "field1,-field2".

So I will write a function like:

JavaScript

My questions are:

  1. what should I write inside the compile_ob function?
  2. what should I return as type?

Advertisement

Answer

You actually don’t need a function to do this. Here is an example:

JavaScript

or you can return a list from compile_ob function and use it with a starred expression.

JavaScript
Advertisement