Opened 6 weeks ago

Closed 10 days ago

#36857 closed New feature (fixed)

Add totally_ordered property to QuerySet to check for deterministic ordering

Reported by: VIZZARD-X Owned by: VIZZARD-X
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: ordering
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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).

Change History (22)

comment:1 by Jacob Walls, 6 weeks ago

Owner: set to VIZZARD-X
Status: newassigned
Triage Stage: UnreviewedAccepted

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.

comment:3 by VIZZARD-X, 6 weeks ago

Has patch: set

comment:4 by Jacob Walls, 6 weeks ago

Patch needs improvement: set
Version: 6.0dev

comment:5 by Jacob Walls, 6 weeks ago

Needs tests: set

comment:6 by VIZZARD-X, 5 weeks ago

I have updated the PR to address the review feedback regarding test coverage.

comment:7 by VIZZARD-X, 5 weeks ago

Needs tests: unset
Patch needs improvement: unset

comment:8 by Jacob Walls, 4 weeks ago

Patch needs improvement: set

comment:9 by VIZZARD-X, 4 weeks ago

Patch needs improvement: unset

comment:10 by Jacob Walls, 4 weeks ago

Needs documentation: set

comment:11 by VIZZARD-X, 3 weeks ago

Needs documentation: unset

comment:12 by Jacob Walls, 3 weeks ago

Patch needs improvement: set

comment:13 by Jacob Walls, 3 weeks ago

Needs tests: set

comment:14 by VIZZARD-X, 13 days ago

Needs tests: unset
Patch needs improvement: unset

comment:15 by Jacob Walls, 12 days ago

Patch needs improvement: set

comment:16 by VIZZARD-X, 12 days ago

Patch needs improvement: unset

comment:17 by Jacob Walls, 11 days ago

Needs tests: set
Patch needs improvement: set

comment:18 by VIZZARD-X, 11 days ago

Needs tests: unset
Patch needs improvement: unset

comment:19 by Jacob Walls, 11 days ago

Needs tests: set

comment:20 by VIZZARD-X, 10 days ago

Needs tests: unset

comment:21 by Jacob Walls, 10 days ago

Triage Stage: AcceptedReady for checkin

comment:22 by Jacob Walls <jacobtylerwalls@…>, 10 days ago

Resolution: fixed
Status: assignedclosed

In 08b4dfc5:

Fixed #36857 -- Added QuerySet.totally_ordered property.

Thanks Simon Charette for the idea.

Note: See TracTickets for help on using tickets.
Back to Top