Skip to content

2026

Sorting Strategies for Optional Fields in Django
February 03, 2026

Introduction

You're building an admin dashboard that displays users sorted by their most recent login. The goal is simple: identify your most engaged users to prioritize them for a new beta feature access.

You sort by last_login in descending order, expecting to see users who just logged in at the top. Instead, every user who has never logged in appears first, pushing your active users (the ones you actually care about) off the first page.

This happens because PostgreSQL (and several other databases) treats NULL as "larger than" any actual value when sorting in descending order. Users with no login history (NULL) sort before users who logged in seconds ago, which is the opposite of what you need.