How to change default default collation behavior from PostgreSQL to Django ORM

PostgreSQL, by default, considers NULLs to be the highest and therefore sorts them first for downstream queries and lasts for upstream ones.

You can change this behavior for each query or when creating an index by specifying "NULLS LAST" or "NULLS FIRST".

How can I use this in conjunction with Django ORM without the need for raw queries? That is, when I add something like qs.order_by("-publish_start") to my query_set, how can I define sorting for zeros? Or, alternatively, after declaring a field / index.

+7
source share
1 answer

I figured out a way that supports DB mechanisms that work anyway (zero as the maximum or minimum value), using extra , making the boolean check null, and when sorting booleans false < true seems to be universal:

 qs = qs.extra(select={'null_start': "publish_start is null"}, order_by=['null_start', '-publish_start']) 
+9
source

All Articles