Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: order_by booleanfield in postgresql

I'm seeing weird outputs while trying to order_by booleanfield in postgresql.

I have a boolean field with default=false and I'm using the order_by(-thebooleanfield) and something I see the True value first which is what I expected but something the False values come first.

Do I need to switch to different type? Or there is a correct way to order_by booleanfield in postgresql?

10x

like image 559
DjangoPy Avatar asked Aug 21 '26 01:08

DjangoPy


1 Answers

Django is ordering by the value stored in the underlying database. I've never found any official explanation in the Django documentation, but there was a Django ticket a while back that explains it:

https://code.djangoproject.com/ticket/19726

Basically, the devs marked it as wontfix because implementing a consistent behaviour between database backends would make them more fragile.

The best way to ensure consistent behaviour is probably to sort the values client-side.

like image 81
Fiver Avatar answered Aug 23 '26 08:08

Fiver