[Django] #36857: Add totally_ordered property to QuerySet to check for deterministic ordering

8 views
Skip to first unread message

Django

unread,
Jan 9, 2026, 2:06:39 PM (6 days ago) Jan 9
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Type: New
| feature
Status: new | Component: Database
| layer (models, ORM)
Version: 6.0 | Severity: Normal
Keywords: ordering | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Currently, `QuerySet.ordered` returns `True` if any ordering is applied,
but this does not guarantee deterministic results. For example, ordering
by a non-unique field (like `pub_date`) is considered "ordered", but the
relative order of records with the same date is undefined and database-
dependent.

This lack of determinism causes issues in pagination and serialization
(specifically for natural keys), where a stable sort order is required to
ensure consistency.

I propose adding a property (tentatively `totally_ordered`) to `QuerySet`
that checks if the applied ordering is sufficient to guarantee a stable
sort. This would verify if the order clauses include a unique field (like
the primary key) or a set of fields covered by a unique constraint.

Note: Similar logic already exists in
`django.contrib.admin.views.main.ChangeList.get_ordering`. The plan is to
extract that logic, make it public/reusable on `QuerySet`, and document
it.

Context: This feature was identified as a requirement for fixing #36750
(deterministic M2M serialization).
--
Ticket URL: <https://code.djangoproject.com/ticket/36857>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 9, 2026, 2:10:45 PM (6 days ago) Jan 9
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Owner: VIZZARD-X
Type: New feature | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* owner: (none) => VIZZARD-X
* stage: Unreviewed => Accepted
* status: new => assigned

Comment:

Accepting without taking a trip to django/new-features because it's a
requirement for a pending ticket (#36750) that could have been worked on
as `Refs #36750`, but we decided to explode into a second ticket for
clearer discussion.

Tentatively assigning to the author of #36750, who I think volunteered to
push it forward.
--
Ticket URL: <https://code.djangoproject.com/ticket/36857#comment:1>

Django

unread,
Jan 9, 2026, 4:21:07 PM (6 days ago) Jan 9
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Owner: VIZZARD-X
Type: New feature | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by VIZZARD-X):

PR opened: https://github.com/django/django/pull/20518
--
Ticket URL: <https://code.djangoproject.com/ticket/36857#comment:2>

Django

unread,
Jan 9, 2026, 4:21:15 PM (6 days ago) Jan 9
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Owner: VIZZARD-X
Type: New feature | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by VIZZARD-X):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36857#comment:3>

Django

unread,
Jan 9, 2026, 5:04:36 PM (6 days ago) Jan 9
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Owner: VIZZARD-X
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1
* version: 6.0 => dev

--
Ticket URL: <https://code.djangoproject.com/ticket/36857#comment:4>

Django

unread,
Jan 13, 2026, 12:34:02 PM (2 days ago) Jan 13
to django-...@googlegroups.com
#36857: Add totally_ordered property to QuerySet to check for deterministic
ordering
-------------------------------------+-------------------------------------
Reporter: VIZZARD-X | Owner: VIZZARD-X
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36857#comment:5>
Reply all
Reply to author
Forward
0 new messages